Destroy Your Enemies — Make Them Explode!
In some of my previous articles, I have demonstrated creating 2D animations inside of Unity for various effects. Today, I will expand upon that concept and use it to create a death animation for the enemy ships!
Creating the animation itself is simple: place all the desired frames in the Animation window and save the new animation.
Now, this animation doesn’t just play at all times, so we need to set up a trigger system to ensure it only occurs when the enemy dies (which is when it collides with a laser or our player ship).
Opening up Unity’s Animator window, we can look at the animation states for the enemy ship.
By default, the animation created will be the default anim, meaning it will play right away. To stop this, we create a new empty animation, then make a transition to our “on death” animation and give it parameters.
Under the parameters tab we can create a new parameter of type trigger, then access it through code.
NOTE: Make sure the transition between “Empty” and your animation DOES NOT HAVE EXIT TIME CHECKED.
Now inside of the enemy C# script we can begin the logic. First, we need a handle to the animator, which we can set as a variable and then use GetComponent to access.
Finally, we can access information on the animator just like we can call functions from other scripts!
Let’s see it in action!