Here I am, knee-deep in my never-ending quest to add more stuff to my game. This time, I want to add shielded enemies that require an extra shot to destroy. Surprisingly, this will only take a small amount of code!
I started by copying the existing player shield object and making it a child of the Enemy prefab. Then I went into the Enemy script and added some code to be able to reference the shield, similar to how it was done for the player script. In the Awake method, I get a random integer between 1 and 10 and if it is 1, the enemy will spawn with a shield. I used a switch-statement just in case I want to add more random enemy types in the future.
In the Start method, I set the enemy shield object to active if isShieldedEnemy is true. Then in the collision logic, I added a few lines to handle the shield. I am only taking the shield into account when the enemy is struck by a basic laser from the player. I want the player’s railgun special attack to ignore the shield, and colliding with the player will also ignore the shield. I also cleaned up the code a little bit by condensing the core enemy-destroy logic into a method.