Any good game needs explosions.
But how does one make an explosion in Unity?
With animations of course!
GamedevHQ provides a custom explosion animation for the enemy character in their 2D game course.
Just like in my article about 2D animations in Unity, I select the enemy prefab, open the animation window, create the animation, and drag in the animation frames.
However when I try to play the game, the enemies explode immediately.
This is because there is nothing stopping the animation from immediately playing. So I need to create a wait state in the Animator that stops the animation from playing before I want it to.
So in the animator I right click and create an empty state.
Right click the new empty state and make it the default state, right click it again and make a transition from the WaitState to the enemy explosion animation.
Click on the transition arrow from the WaitState to the EnemyDestruction_anim, and in the inspector, untick “Has Exit Time” and reduce the “Transition Duration” to 0. This will make the enemy exploding animation play immediately when requested.
To actually request it, however, the transition needs a trigger.
The trigger is created in the parameters section of the animator window, and then the transition needs the parameter assigned as a condition.
To activate the animation, I now only need to get the Animator component on my enemy, and set the trigger.