Visualising the player shield

Daniel Kirwan
3 min readApr 21, 2021

--

I previously wrote an article about creating a player shield and how to animate it, which you can read here.

I now want the shield to have a set amount of times it can be hit before it gets disabled. But I also need to let the player know how many hits it has left in a visual manner as well. I am in two minds in how to do this so I have implemented both for now.

The first way is to have a UI element similar to the health that appears when a shield is picked up and depletes when the shield is hit. Then next was to change the colour of the shield when it is hit so that it looks like it is losing energy.

The easiest of these to implement is to change the colour of the shield when it is hit. It takes one line.

You will need to find the object that has the renderer you want to change, for me that is the child object that is attached to the main shield object that is attached to the player. You can then get access to the material of the renderer and change the colour.

In my Damage method for the player I am checking if the shield is active. If it is I am then checking the number of hits it has left. Depending on the hits the shield has left I am changing the colour of the material, I am then updating the UI images for the shields as well. I am then returning out of the Damage method so that the player doesn’t take any damage.

The Updating of the images takes a little longer to implement as you will need all of the images you want to change to before you begin.

We set up an array of Sprites that we set up in the Unity editor.

All of the elements in the image are for the sprites that you want to swap between, the Shield Image is the UI image gameobject that is attached to the canvas and is where on the screen the sprites will appear.

Using the shieldImage gameobject in the UIManager script.

What we’re doing in the image above is changing out the current sprite with a new one. The one that we change to is the number of shieldHits that are being passed through from the Player when the shield takes damage.

And that’s it for this article. Come back again for more about games in Unity.

--

--

Daniel Kirwan
Daniel Kirwan

No responses yet