Unity: Trigger an animation through code

Let the enemy explode in style!

Timo Schmid
5 min readApr 26, 2021

At the current state of the Space Shooter, enemies and laser do interact with each other and trigger various events. However, the effect is not really a great one for the eye. Both GameObjects are just disappearing without anything special. That’s not really great, is it?

Let’s change this and create a cool-for-the-eye explosion effect to make the look-and-feel even more enjoyable!

Creating the animation
Using the provided assets from GameDevHQ, we have every sprite we need.

  • Open up the Animation window
    (Window > Animation > Animation)
  • Click on the enemy to create a animation for it
  • Click on “Create” on the Animation window
  • Name your animation in a a way to find it easily
  • Enable the keyframe / recording mode by clicking the red record button
  • Drag in every sprite you want to have in the animation

Here’s a GIF showing the whole process:

DO NOT forget to apply the added animation to every single Enemy we want to instantiate! To do this, click the Enemy > Overrides > Apply All

We just created the animation. Let’s start the game to see it in action:

As you can see, things are weird. The animation is playing before we even hit the enemy. It’s even looping!
That’s not what we intended to do. Let’s look up what’s causing this.

Checking the Animator window
Let’s check the animator window and take a look at what’s going on. Open it up by choosing Window > Animation > Animator

The Animator window

As you can see, we have four blocks here, which we will call “States”. Let’s take a closer look at the Entry state for now. There is a orange arrow pointing to the enemy explosion animation we just created.

Exactly here lies the problem! The Entry state is called as soon as the game is launched. Therefore, as soon as we hit play in the editor, it will directly play the animation. This can come in very handy in some scenarios but certainly not in this one. To solve this, we need to create a new, callable state and create a transition tot he animation.

Setting up the new state

  • Right-click the gray grid area
  • Choose “Create State”
  • Click on “Empty”
  • Rename the new state if needed

Having a new, empty state, we now need to make this the default state. Otherwise, we would have the same result as begore and the enemies would explode before we hit them.

  • Right-click the “Empty” state
  • Choose “Set as Layer Default State”

Implementing the trigger
At first, we need to create a transition to the explosion animation. Otherwise, the animation would never play.

  • Right click the “Empty” state
  • Select “Make Transition”
  • Move the mouse to the created enemy explosion and left-click

As everything is set up now, we can create the trigger. We keep it simple here, therefore, we won’t use a Animation Layer. We use Parameters instead.

  • On the Animator window, click on “Parameters” at the top
  • Click the + icon at the top right
  • Select Trigger
  • Rename the Trigger

To only play the animation when it is triggered, we need to create a Condition for it.

  • Click on the transition arrow
  • In the Inspector, scroll down until you find ”Conditions”
  • Hit the + icon at the bottom right

Activating the trigger through code
We need to access the animator. Same procedure as always.
Variable > Handle > NullCheck

We want to do the following things whenever the enemy gets destroyed:

  • Destroy the Collider to avoid another hit during the animation is playing
    Think about the reference and handle!
  • Set the speed to 0
  • Trigger the animation
  • Destroy the enemy after the animation has finished.

To keep the script modular, let’s create a method for this:

Lastly, we have to call the method inside the OnTriggerEnter2D method. This is how it will look in the end:

Getting rid of the loop
At the very last, we should make sure to disable the looping of the destroy animation.

  • Click the EnemyExplosion_anim file inside the Project view
  • Untick the “Loop time” checkbox

Let’s take a look at the end result:

--

--

Timo Schmid

The mission? Becoming a game developer! RPG is the dream! Writing down my journey here for me and for everyone interested. Thanks for showing interest :)