Building stronger shields in Unity!
Objective: Implement a lives system with shields and a visual representation of its current life.
So far, if you follow along, we have implemented a shield powerup for our player and if the player gets hit once the shield is gone. We want to upgrade the shield so it can take three hits and give a visual representation with how strong the shield is in its current state. With every hit the shield will change size and color.
To begin we need to open the script for the player and create a variable to represent lives for the shield, like the player.
Now we need to go to the method that deals with the activation of the shield.
Now we need to add the new variable for the shield lives and set it to the max shield life. This way, every time this method is called the shield’s strength will be set back to its max.
Now in the method that deals with damage we can set it to subtract a life every time we get hit.
This will work however there is no indicator what the current strength of the shield is. Let’s start with a size difference. We are going to manipulate the scale every time we take a hit. To start we need to hard set the scale when we activate the shield.
Now when we pick up a shield powerup the scale will be set to its normal scale.
We want to use if statements to set the scale size depending on how many shield lives we have.
This way if we take one hit, meaning our shield lives are 2, then we minus the scale by .75 on the x, y, and z axis, and again by .25 if we are hit twice.
Now we can also set a color to help indicate the strength of the shield using Get Component. Blue for full power, yellow after one hit, red after two, and then it’s gone. So, let’s set the color to blue when the shield is activated.
Now, no matter what the current state of the shield is if it’s activated it will reset to the blue.
This will change the color to the desired color depending on the lives the shield has. This is it, now you shield can take multiple hits, and show when it’s at full strength or when it’s been weakened. You can always add more lives or less depending on your own desires for you game. Don’t be afraid to experiment with your code, and I’ll see you in the next tutorial.