Everything is better with EXPLOSIONS! Especially when you are destroying enemies. In this article, I will talk about adding an explosion animation to my enemies when they are hit.
In the enemy Prefab, you will need to create a new animation. Looking at the Animator, which controls the animation, by default will go from Entry to the Enemy Explode Animation. This means, as soon as the enemy is instantiated the Enemy Explode Animation will run and even though EXPLOSIONS are great, we want to be in control of when they happen. To prevent the animation for automatically running we will need to create an empty state, so when it starts, it transitions to basically nothing and from there we can control transition to the Explosion Animation.
Adding a transition from the empty state to the Enemy Explode Animation, we can add a Parameter that will allow for a certain condition to be met before running the animation. You can have four different types of Parameters: Float, Int, Bool and Trigger. We are going to use a Trigger parameter, so when we want to use the animation we just have to set the trigger to run it.
With the parameter we can use code to trigger the animation, so we will jump into the Enemy script. First, we will need to get a handle on the Animator component so we will create an Animator type variable.
Then, on the Void Start we can get the animator component and we do not need to find it since we are already on the Enemy GameObject. Now, the Animator component is store in the variable we should do a Null check to make sure we actually got it.
Once we have the Animator Component stored in our _anim variable, we can now call the Trigger Parameter using .SetTrigger and pass in the name of the Parameter (“OnEnemyDeath”). The Explosion Animation can be misleading, because it seems everything is destroyed, but all of the colliders and physics are still there and are running; The image has been replaced.
After the animation starts, we will want to set the Enemy speed to 0 so it and all of the rest of the enemy components stops moving. Then will we Destroy the Object, which will get rid of the Collider. If this wasn’t done, an invisible box collider will continue to move and can still interact with the Player. There is still an issue. Going through the the line of code again, the animation is played, Enemy speed is set to 0 and it is destroyed. All of this happens very quickly, to the point that the enemy is destroyed without animation have the chance to run. We can pass in a float value, that will act as a delay to when the enemy will be destroyed and looking at the length of the animation, it is about 2.8 seconds.
Lastly, looking back at the Transition from the Empty state to the Explosion Animation, we want to add the Trigger condition and we want to set Has Exit Time to false. If this was still true, the Empty state would have to finish playing before making the transition to the Explosion Animation causing a bit of a delay. Making it false has it instantly make that transition.