One Shield, 3 Times Protection- Game Dev Series 33

S.J. Jason Liu
Nerd For Tech
Published in
3 min readMay 28, 2021

Objective: Make the shield powerup has 3 levels of strength.

Shield powerup can only prevent 1 attack is too harsh to Player. We can create a 3-times-proof shield instead of single proof.

First in my mind is how to present the levels of shield. I think decrease the opacity of shield sprite would be obvious to see the difference. And also the opacity value would be a good variable to identify the status in code.

To extract the opacity value insides a sprite, we will need to get the color value in SpriteRenderer component.
Read the document of Color in Unity, there are 4 values in it: R, G, B, and A. The last one “A” means alpha, which is opacity we need to change.

Create a variable of color then locate the component in Start().

We need to check the value range of alpha color in the editor.

In the Inspector, you will see the alpha range is from 0 to 1(if your range is 0–255, don’t worry, the following code is the same). Which is what we are going to change in the function.
We will set the alpha of shield decrease 25% each time when we were attacked. This value is nearly about 0.3. Since the last state would shut down shield directly, let’s set the value to 0.4.

Set another if statement inside the original if statement of Damage().

In this if statement, when the player take the third time of damage during shield protection, shield will disabled get counts as last protection.

We also need to set the value to 1 when Player gets the shield powerup.

Then we can test our triple protection shield!

--

--

S.J. Jason Liu
Nerd For Tech

A passionate gamer whose goal is to work in video game development.