Adding Shield Strength and SpriteRenderer: Space Shooter GDJ 19

Brett Blandford
2 min readJun 4, 2021

I recently added shield strength to my Space Shooter game. The shield powerup can now take three hits before it is destroyed. Each time the shield is hit, it’s alpha is decreased which makes the shield appear weaker.

The way the shield visualizer functionality was originally set up was when the player was hit (LoseLife() method), there would be a check to see if the shield was active. If it was active, the player wouldn’t lose a life and the shield would disappear using SetActive(false). Now that the shield has a more complicated behavior, I made a method called ShieldVisualizer() to call within the if check inside the LoseLife() method.

Here is how the ShieldVisualizer() method itself:

The switch statement uses the _shieldLives variable which is how many times the shield can get hit before it gets destroyed (3). _shieldLives is set to 3 when the shield power up is collected. So this means if the player has a shield then collects another, _shieldLives would equal 3 again. Inside each case, the shields SpriteRenderer is accessed by the _shieldRenderer variable which allows the color to be accessed as well. Here the Alpha value can be changed, in case 3 it gets reduced to .5 and then incase 2 reduces it to .2. In case 1, the shield game object is set to false, then the Alpha is reset to 1 for the next time a shield power up is picked up.

Now the shield can get hit 3 times, each time reducing the alpha to make it look weaker, and make it easier to fight back the enemies!

End of GDJ 18!

--

--