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.

  • Click on the sprite you just moved into the Hierarchy
  • Press the W key on your keyboard to move the GameObject around
  • Move the sprite around with the two arrows shown

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:

  • Open up the Animation window
  • Click the sprite we use for visualizing the damage
  • Create an Animation Clip
  • Enable the Keyframe recoding mode
  • Drag the sprites into the timeline

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!

  • Move the duplicate to the right side
    Tip: As the player is centered, you can just remove the ‘-’ of the Y-axis coordinate on the Transform component.
  • Rename the duplicate.
    You can easily distinguish the duplicate from the original by looking out for a (1) suffix.

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.

  • Having all three lives, we don’t need a damage visualizer at all
  • Two lives left, we enable the left visualizer
  • At one remaining life, both visualizers will get enabled.
  • Having no lives left, the player will disappear. And so will the damage visualizers as well

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:



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
Timo Schmid

Timo Schmid


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 :)