Day 35 | Shield Strength: 2D Space Shooter Series 03
In this article, I’ll explain how I added more depth to my shield boost ability.
The Added Feature
To add more depth to my shield boost powerup, I gave the shield 3 lives. This means that while the shield is active, the player’s ship can get hit 3 additional times before damage affects the player’s health.
To implement this, we assign a variable to be the shield’s additional lives and keep track of the number of times the player’s ship is hit. If the player is damaged, we can check if the player’s shield is on. If it’s on, instead of reducing the player’s health, we can reduce the shield’s health until it reaches 0. So, in the Damage method of our script, we can add this logic.
private void Damage()
//Update shield lives in uimanager
if (_shieldLives == 0)
_isShieldActive = false;
After giving the player more help, let’s make sure to update the player on the current status of their shields. We’ll use a similar methodology to how we set up representing the player’s lives.
We’ll line up 4 image objects next to each other. 1 will represent the Shield, and 3 will represent the health of the shield. We’ll then refer to the 3 images that represent the health of the shield in our script. We’ll put them into an array and call the variable _shieldStrength.
Depending on the shield’s health, we’ll change the transparency of each of the images whenever the player receives damage. Additionally, we’ll preset the color we want by creating variables with SerializeField attributes and assign the colors in the editor. We’ll call the variables _shieldColorOn and _shieldColorOff.
Now, we have a shield feature with UI that will give players feedback on the status of their shields!