Adding the Player Shield
Game functionality
Almost identical to how we set up the Player Health, Player Health Bar and Health Power Ups, in this article we’ll look at introducing a Player Shield System as well as linking it to a Shield Bar UI Element, and creating new Shield Power ups.
Creating the Player Shield System
To start we create a few new variables to initialise the Player Shield and to check if it’s active or not:
In Start() we set the current shield to the max shield:
Next we create a method allowing us to damage the Shield:
Additionally in Update(), we execute another method responsible for deactivating the Shield when it depletes and in general for Shield behaviour outside of the DamageShield() method:
Implementation
Now that we have a shield system in place we can basically mitigate the shield damage into normal damage for as long as the shield is active.
Creating the Shield Bar
To create the Shield Bar I just duplicated the Health Bar, renamed it and inverted it on the x-axis. I set the fill Image color to blue.
In the UI Manager.cs, very similar to the Health Bar, we create two new methods (having a reference for it just like we have for the Health Bar); one allowing us to set the initial shield, and one to update the current shield. This time we wont be using a custom gradient as I don’t think it’s necessary for the Shield.
Creating Shield Power ups
Likewise very similar to how we created Health Power ups, we can now manufacture the logic for Shield Power Ups.
Then we have to update our PowerUp Enum and the OnTriggerEnter() method in the Item.cs Script:
Now we can create three new prefabs that we can spawn: