Animating Player Damage (Game Dev Day 25)

Jack Leavey
3 min readSep 10, 2021

--

Objective: Add visual damage to the player when they lose a life.

Currently we have a working lives system that displays your remaining lives in the UI. However, we can take it a step further by adding graphical damage to the player model.

The UI works, but we can make it better.

Fortunately, this is incredibly simple to do. First, we need to create the animations for the damage.

Create a new animation file (Player_damaged_anim). Then drag your frames into the Animation window in Unity.

Animation window should look like this after you drag in your frames.

Now you have the animation, but we need a gameobject to assign it to. Drag the first frame from the animation into the Hierarchy to create the gameobject. Make sure you make this object a child of the Player object.

Do this process again or just duplicate the object and place both of them where you want them to show on your player model.

NOTE: Make sure you are placing these object higher in the sorting layer than the player, or they will not display properly. Sorting layer edits are done in the Inspector window while you have the applicable object selected.

Check the Sprite Renderer options if you have graphical issues.

I named mine Right_Engine and Left_Engine.

This will serve as our visual indicator that the player is damaged.

Now we attach the animation to these objects. Because these objects are children to the player object, we now just need to identify them in the Player.cs script and then instantiate them when we take damage.

Make two variables, one for each engine.

Make sure you SerializeField.

Now drag each object from the Hierarchy into their slots in the Player.cs component of your Player gameobject.

You are adding these to the Player Object under the Player (Script) component.

Finally we need to instantiate each of these objects for our different lives values. In our Player.cs script, we have a method to handle damage already, so we can just add to it.

Use an “else if” statement to instantiate each damaged engine graphic for the 2 and 1 life values.

Let’s take a look at the final product.

Visual effect showing our engines breaking when taking damage.

--

--

Jack Leavey
Jack Leavey

Written by Jack Leavey

I am a software engineer with years of experience branching into game development, specifically in Unity. Follow along for guides on creating game mechanics!

No responses yet