Day 36: Shield Strength
Objective: Allow for multiple hits to the shield
To start, we will need a reference to the current strength the shield has and the max. We need a max so the player can’t collect 100 shields and be invincible. We’ll create the variables on the player script.
Where we assign the bool _isShieldActive, is where we would want to check to if we are at max shield strength or not. If we aren’t at max add one to it.
We add to the strength of the shield, now it’s time to take away. In the damage method, when the player gets hit we’ll check to see if the shield strength is more than 0 and return.
That’s how we give the shield strength, but now time to show some visual of the strength. Today, we will talk about adjusting the alpha of the shield to show the strength. We’ll start off by grabbing the Renderer from the shield.
Now we have a reference to the renderer, we will need a new method. The reason for a new method is, the method will be called from two different locations, when we collect the shield power up and when the player gets damaged. We don’t want to have the same code in multiple spots.
We’ll call the method ShieldDisplayStrength. Inside it we will use a switch statement. For each case of the shield strength we’ll change the current shield color to a new one with a different alpha.
Now that we have the method done, we need to call the method for it to work. Where we add to the strength is a good place to call this method.
The other place to all it would be where the player gets damaged.
This where give the player some feedback on how strong his shield is! You now have a shield that can be stronger and a display to show it.