Space Shooter: Enemy Shields

Jay Barnes
3 min readDec 4, 2023

--

Once again we’re going to be expanding our enemy roster by adding another new type of enemy, using class inheritence that we’ve detailed in a previous article.

The type of enemy we’ll be creating today will be a tankier enemy that’ll be able to take more than one hit with the addition of a shield.

To setup our new enemy we duplicated our base enemy gameobject and created a new prefab out of it, changing the color of the gameobject and created the shield by using the same shield powerup visualizer we used on the player, changing it’s size and color.

Next, we created a new script for our new enemy gameobject, naming it defender and inheriting the Enemy class.

Inheriting the enemy class gives us access to the features of our Enemy parent class, but there’s a few modifications we want to make to chang up the behaviour of our Defender.

To start, we want to switch up the movement a bit so we’ll add an override to our CalculateMovement() method.

With this code, our gameobject doesn’t just move straight down, it moves slightly down and to the right. Once our gameobject reaches the other side of the screen, it resets at a random position on the x between -8f and 1f, at 7f on the y axis.

To implement our shield, we need two new variables, one to serve as a reference to the shield itself, and another bool to check if the shield is active or not.

After populating our enemy shield in the inspector, to make use of our two new variables we’ll need to override our enemy collision code.

We first want to check if our shields are active, and if so we set shield active to false, deactivate the shield visualizer, and return out of the function. If our shields aren’t active we move on to our base collision logic from our Enemy parent object which looks like this:

With that, our Defender type Enemy is ready! The last thing we need to do is add it to our SpawnManager and test our game.

--

--