Phase 2: Enemy Shield

Objective: Allow the original enemy have a single hit shield

We start by adding the shield to the enemy prefab and setting the scale and color to match. Then open the enemy script and add a serialized game object for the shield and a private bool called active shield and set it to true since the enemy will start will the shield in place.

Next in the OnTriggerEnter method, within the if other is player check, run an if statement to check if is not the red enemy and if active shield is true then set active shield to false, shield visualizer setActive to false and call the players take damage method before returning and not destroying the player.

We run the same if statement within the if other is laser tag, only this time we don’t call the take damage method we destroy the other game object(laser) before returning. By using this check we can isolate the different enemies without recreating multiple scripts for each enemy.

Part of if other is player OnTriggerEnter method

As you can see in the above script only the original enemy while having an active shield be affected. The red enemy or original enemy without a shield will pass over to the next block in the method. The return element is key here as it stops the rest of the method from running so it doesn’t destroy the enemy or the red enemies EMP pulse.

In the next post, we will allow enemies lasers to destroy collectables.