Our Player can become invincible when they pick up a Shield Powerup, but it only lasts one hit. We’re about to increase the difficulty of our Game, so I think it’s fair that we give them a little more leeway in terms of shield health.
In this article, we’ll give our Player Shield some health, and have that reflect visually by changing the colour and alpha!
Creating the Shield Health
To have health, first we must create health. Or at least a variable for it!
Inside our Player script, we’ll create an int for the shieldHealth, and also create a reference for the SpriteRenderer of the Player Shield so that we can change the colour with every Shield Health lost.
Now when we activate the shield, we’ll set it’s health back up to full (3).
This will also set the Shield Health level back to 3 if you pick up a Shield Powerup while having a shield powerup already active. So be wary of that if you do not want that functionality. We’ll also set the colour to be default White.
Be sure to connect the Player Shield in the Inspector to the new SpriteRenderer reference that was created as well!
Decreasing the Shield
Next we’ll be decreasing the Shield Health when the Player gets hit and the Shield is active. Inside our Player script in our DamagePlayer() function:
For the Color, the first 3 values relate to the RGB values, and the fourth value relates to the Alpha transparency.
It’s easy as that! Our Player now has more chances not to be destroyed!
In the next article we’ll be giving our Player an Ammo Count. Decreasing their ammo with each shot, and create an Ammo Pickup to gain more Ammo for the oncoming battles!