Visualizing Player Damage.

Jason Schneider
3 min readAug 12, 2021

Now that we have the lives counter and score showing at the top of the screen, it is about time to add visual clues as to the damage we have sustained.

Once again we can use Animations to accomplish this. First we need to drag the sprite we want to use as damage onto our Player object in the hierarchy. Rename, resize and adjust position to suit, could be similar to below:

Then we need to click on the sprite in the hierarchy and add and save the Animation we want to use to it. Running the game will give:

Now we can right click on the newly created Animation in the hierarchy, and Duplicate it. Moving it to the opposite side and running game will give:

Awesome, now we just need to have these Animations appear when we are damaged. In order to accomplish this we need to add two variables to our Player script.

Since variables are of same type can have them separated by a comma, no need for separate lines.

Save the script, jump back into Unity and drag the Animations into the newly created fields on our Player object. Now that the Player script has access to the Animations we can turn the Animations off by adding the below to the Start method:

Next, we need to code when we want the Animations to be turned back on. This is of course when we are damaged, so, if we add the below to the Damage method in our Player script; after the :

Saving the script, jumping back into Unity and running the game will give:

Fantastic, now both the lives counter and the damaged Animations work.

--

--