Visualizing damage on the Player

Hey! Your ship is hurt!

We have set up many things of the UI now. We have the score system, the health visualization via sprites and we just added enemy explosions! Now it’s time to add some details on the player. Let’s visualize the damage!

Positioning the sprite
We begin as always when we create a new animated sprite. The very first thing to do is moving the sprite from the Project view into the Hierarchy. Afterwards, we need to position the sprite where we think it fits best. We move it to the very bottom of the spaceship.

This can be your player spaceship after moving the sprite around:

Animating the sprite
We animated some sprites up until now. We need to create a animation here as well in order to make the effect more vivid and natural. The procedure is the same as always:

This should be the end result:

Duplicate the sprite
We have three lives. If we get hit once, we will show one damage visualizer. With only one life remaining, we will show it on the right side as well. For this, we need another damage visualizer.

You don’t need to re-create another one from scratch like you did just now. Duplicating works as well and saves you (a lot of) time! Just hit CTRL + D (or CMD + D if you are on a Mac) and the GameObject gets duplicated!

Creating the trigger logic
Everything is set up now inside the Editor. Now it’s time to create the logic for the game!

We have three lives. If we get hit once, we will show one damage visualizer. With only one life remaining, we will show it on the right side as well.

This is all we need to create the method. But instead of using multiple if-else statements, we will directly use a switch statement.

We check the current lives and want to jump to the part of the method representing the current lives. We have four possibilities. Three lives, two lives, one life, zero lives / game over. Therefore, we need four cases.

Unity makes enabling or disabling GameObjects easy.

The method representing our logic will look like this at the end:

In order to update the visualizer, we need to call the method every time we take damage. Therefore, we have to call the method inside of the Damage() method as well.

To prevent any issues when you build and release the game, you should also call the method in void Start(). With this, you make sure to disable the visualizers regardless of the state the GameObjects have had inside the Unity Editor. If you left the right visualizer active inside the Editor, it would stay enabled in the built game as well until you take damage. This would confuse the player and is not something we intended to do.

The finished end result will look like this inside the game:

The mission? Becoming a game developer! RPG is the dream! Writing down my journey here for me and for everyone interested. Thanks for showing interest :)

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