Buffing up Enemies - Activate the Shields!
It is time to increase the challenge of the game. I will be adding more enemy types, behaviors, and the weapons they use over the next few days… But I wanted to start with something random since all games require a particular random element to make the player extra frustrated, right? - and that is giving them shields!
Randomly spawn enemies with an active shield power-up!
First up, we need to create a new Prefab GameObject as a Child to the ‘Enemy’ Prefab - this will be the ‘Enemy Shields,’ which I have decided to set the enemy shields to green color, as are their lasers.
Now open up the ‘Enemy’ script. Here I need a reference to the GameObject, which I will populate in the Inspector, and a boolean variable, which will control whether the shield power-up is active or not.
As you may have noticed, I also added a new method call ‘SpawnWithShield’ in the ‘Start()’ method. This method determines whether the Enemy prefab that is being Instantiated will spawn with a shield or not. I have given this a random 25% chance.
With this done, the shield is activated. A method for deactivation is also required - pretty straightforward stuff.
Finally, an adjustment to the ‘OnTriggerEnter2D’ method is needed. When the shield is active, and a collision with a laser happens for the first, I am simply turning off the shield.
However, if the collision is with a player, I am deactivating the shield and damaging the player. Next, I am checking if the game is over or not - if it is, the enemy survives. If it is not, I am, again, damaging the player and destroying the enemy as well.
Thus, colliding with an enemy with a shield power-up could cost the player two lives! The player now needs to be extra careful when dealing with such an enemy!
But that is it for now, thank you for reading and feel free to follow me for more articles - and as always, good luck and see you next time!