Death Animation

Adding a Death Animation to our Enemy and Player upon Death.

Now that we have finished with all the enemy basics, we can finally finish off the Enemy AI by adding the Death animation for all enemies when their health has reach zero.

Preparing the Death Animation:

Just like how we have been slicing and importing the sprites into the dope sheet of the animation. We will follow the same procedure for the Death Animation by slicing the sprites into individual cells and saving a new animation state.

When setting up the animator state, make sure that the loop Time is checked off.

Setting Up the Transitions:

Since the Death state can be trigger at any moment at time, we can place the transition starting from the “Any State” to the “Death state”. The condition to toggle this animation would be a trigger condition which we will name “Death”. As usual make sure that the transition duration is set to zero and that we don’t need an exit time.

The player death transition setup.

With the player death animation setup completed, we can go ahead and apply the same formula to the enemies.

Death Animation script:

To toggle the Animation via script, we already setup the Damage method which destroys the game object when Health is below 1. We are going to use this method and add in the animator trigger condition.

What we need to is first toggle the animation to trigger the death animation, then we also need to stop the enemy from moving back and forth. Therefore we will need a bool to control the movement, if the enemy is dead equals true, then movement will not happen.

When health his less than one, the death animation will trigger, and the bool will be set to true. In the parent Enemy script we will have a protected bool which the child scripts can access. By default this will be set to false.

In the virtual update method, we will condition the movement method by setting the is Dead bool to only false, to allow the movement.

If we return back to Unity and test out the script, you will noticed that once the enemy falls the collider is still active. This allows the player to continuously hit the enemy and repeatedly and toggle the death animation again.

To fix this we must deactivate the collider when the bool “is Dead” is set to true. This can be added in the virtual void update on the parent Enemy script as a Else If statement.

Now when the Enemy is down, the collider is turned off and the player won’t be able to toggle the death animation over and over again.

--

--

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
Simon Truong

Simon Truong

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.