Animating Player Damage (Game Dev Day 25)
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.
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.
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.
I named mine Right_Engine and Left_Engine.
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.
Now drag each object from the Hierarchy into their slots in the Player.cs component of your Player gameobject.
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.
Let’s take a look at the final product.