Creating Spaceship Shield Strength Feature in Unity
Improving the shield powerup
Objective: Make Shield take up to 3 hits and change the color in every time it is damaged
The original shield consisted of a game object disabled and once the player picked the powerup we activated it until the player was damaged.
In order to create this feature I made the shield a prefab and deleted it from the player.
Then I created a variable in the player script to have the reference to the prefab and wrote a function to Instantiate a shield.
Then I added a line to check is the player already have a shield then destroy it and create a new one, next make the shield child of the player.
At this point we have a indestructible shield, now I have to continue with the damage feature for the shield.
I created a shield script and attached it to the prefab. Then in the script I created a variable for the lives of the shield and a function to subtract a live when the shield is damaged.
Now in the enemy script I check for the collision with the shield and called the Damage function.
In order to indicate the shield was damaged in a visual way I created a variable to get the sprite renderer and add conditions in the damage function to change the color of the shield depending on the remaining lives.
With this change we can see how the shield is damaged.
Ok everything looks good, but there is a bug. When the shield collides with an enemy laser it don't suffer any damage.
To fix this I added the function OnCollisionEnter2D in the shield script to check for the Enemy Laser and call the Damage function.
And that’s it!!! We have a completely functional shield strength feature in the game.