Visualizing Shield Strength
The next feature is one that increases the strength of the shield powerup. Right now it can only take one hit of damage before being deactivated completely.
Instead, it would be much better if it could take multiple hits, as well as having some visual indication of taking damage.
To create this effect, we need to add and change some shield related variables. Instead of controlling the shield through a boolean, we can use the integer _shieldHP instead, which starts at _shieldMaxHP and decreases as it takes damage.
We modify OnTakeDamage to use a new helper function “ShieldDamage” which will decrease the hp of the shield and handle other effects as a result of the damage.
The Color.Lerp is a very helpful function which finds a middle-ground between two colors. In this case, as “fractionOfShieldRemaining” gets closer to zero, “newShieldColor” will be less like the original shield’s color, and be closer to black, which causes the shield to look faded. No matter what the _shieldMaxHP is set to, this will always gradually decrease the brightness of the shield visuals as it takes damage.
Finally, a small modification to ToggleShields. When shieldOn is true, we want to set the shield to its maximum strength, and reset the color of the shield to be full brightness.
Now we can see (literally!) the shield able to take multiple hits, and decrease in brightness as it does so.