Feature: Improved Shields

This article will use the shields from a previous article and have them take more than one hit before deactivating. To show how many hits the shields have left, we will change the Color of the shield sprite.

In the Player script, we need to add a few variables. First are two ints, one to hold the total number of hits the shields can take, limited to a range between 1 and 3. The second to keep the current strength of the shields, so we know how many hits are left to block.

Now, we need some variables to change the Color of the Shield to show the number of hits left. The first thing we need is a reference to the SpriteRenderer on the Shield, so we know what to change. Next, we need three Color variables to hold the Colors to which the Shield will be set. The first is to keep the Base Color of the shield sprite image, which is Color.white. The second is the Color the Shield will turn to when it is down to 2 hits. The last is the Color the Shield will turn to when there is only a single hit left before it is deactivated.

In Unity, the Shield Strength is limited between 1 and 3, and you can change the Second and Last Shield Colors.

Back in the Player script’s Start, we need to get the SpriteRenderer on the _shieldVisual and set _shieldRenderer to equal it. Now we can use _shieldRenderer to change the shield Color.

Next, create a new method that we will use to change the Shield’s Color with an int argument for the Shield’s strength. Inside the method, we will use an else if chain to change the shield Color based on the passed in shieldStrength. If the shieldStrength is equal to 3, we will set the Color of _shieldRenderer to the fully charged shield Color. Else if the shieldStrength is equal to 2, we will assign the shield Color to the second shield Color, and for everything else, we will set the shield Color to the last shield Color.

In the ShieldActivate method, set the _currentShieldStrength to _shieldStrength, then call the ChangeShield method and pass in the _currentShieldStrength. That will make it so when the Shield is first activated or when the Player collects another Shield Power-Up, it will restore the hits the Shield can take and change the Color back.

In the ChangeLives method, inside the if statement that checks for the Shield being active, subtract one from the _currentShieldStrength, then check if it is still greater than 0. If so, call the ChangeShield method and pass in the _currentShieldStrength. Add a Debug.Log to print to the Console the Shield was hit. Finally, tell the method to end with a return. That will take a hit from the Shield and change the Color of the Shield if it is not out of hits.

With the code in place, the Shield now can take three hits before deactivating, changes Color as it gets weaker, and can be recharged back to full by another Shield Power-Up.

Unity Developer, Software Engineer, Game Designer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store