Upping the Challenge: Giving the Enemies Shields

Thomas Kesler
4 min readJun 16, 2021

The goal for this article is to get Shields on our Enemy units. And while it may be a good option later to add Shields permanently to the units for a higher Difficulty setting, right now, we will set it up so that each unit will get a single shot Shield at random when they Spawn or Respawn. And we will increase those odds based on the current wave.

If you haven’t been following along, the Shield effect is from our article, Raise the Shields. To give it a different look, we changed the Color and rotated the Shield GameObject in the prefab by 90°. The code I’m about to share can be applied to each of the Enemy types while only changing settings in the inspector afterwards.

We’ll start by adding the variables we will need. _shieldChance will be used by the Designers to determine the possibility a Unit gets a Shield on spawn/respawn, ideally the harder the Unit the less likely they will get shielded.

_shieldActive will be used by our Hit Detection to determine if damage is done or not.

_shield will be the container for our Shield GameObject and will allow us to turn it on and off.

_shieldRenderer will allow us to access the shader and generate the hit and dissolve effects.

In Awake(), we just need to assign the Renderer Component to _shieldRenderer variable. We do this in Awake() because it will be called whenever the script’s Object is set active. This is how we call pull an object from the Pool and will cause it to reacquire the Component each time in case is lost for some reason.

In Respawn(), we want to ‘roll’ to see if the Unit gets a Shield for this next pass. We check a random number (RNG) against _shieldChance multiplied by the Wave, which we can get by querying the SpawnManager. It the ‘roll’ is under the chance, the Shield gets activated and shut off if not. We want the Else portion of the IF/Else in this case because a Shielded Unit could get past the Player in a busy play field and we will want to make sure that it gets reset if it doesn’t get the Shield for the next pass. This will give the appearance that the Units aren’t repeating or that their Shield tech is weak or new. It depends on how the Game’s story tells it.

In the OnTriggerEnter(), We want to add the condition that the Shield may or may not be active. Since the primary effect occurs when the Shield is not Active, we will make the the condition for the IF/Else statement. Then in the Else, we can run the code needed to dispose of the laser and turning the shield off, or in this case Powering it down.

We will want to go through the rest of OnTriggerEnter() and make the same changes to the other hit conditions that would normally damage or kill the Unit.

Now that we are done with the changes and additions to existing Methods, we can work on the new stuff. Here we have two Methods to turn the Shields On or Off. These will be used by the Respawn() mainly to control whether it is active or not. As you can see, We are setting the active state of the Shield Object, as well as setting the power variable in the Shader.

In the ShieldPowerDownRoutine(), we are using very code to what we used in the Coroutine of the same name in They’ve hit the Shields. The key thing to note is that we are not worried about powering down in stages. This version of the Shield only has one hit, so when it receives damage, we want it to go away. But we want a bit of flair, which most of the code here is for.

The only thing left is to Apply the shield to the different Enemy Units. To work this quickly, I suggest two things. First make a prefab from the Player’s shield. Second, Duplicate the Shield Material and rename it to EnemyShield. Then you can set different colors and variables with out worrying about changing the look of the Player’s shield. Now when you drag the Shield Prefab into the Unit’s Prefab, you can apply the New shader to it. You can duplicate the Shield Shader as many times as you like and give different looks to each of them.

Let’s see what you can do with it.

--

--

Thomas Kesler

A Unity Developer with a fondness for Fantasy games and the challenge of pushing boundaries.