Stop, drop, kaboom! — A quick guide to triggering animations in Unity.

Luke Duckett
4 min readApr 18, 2021

--

The best part of blowing up an enemy is the experience. No, just joking, we all know it’s the great fireball we see.

I bet we can all remember back to the first time we shot an RPG at a tank and watched as that bullet sponge went up in a fiery ball of death. Well, that is what we are going to be doing in this article.

First of all, if you need tips on animating sprites you can check out my article. so we will be going over that part pretty quickly in this article.

First thing is to find/create/buy yourself a nice explosion animation you are going to use, import it into your scene and attach it to your enemy.

This will automatically create an animator component on your enemy if you do not already have one. That is just step one. If we continue from here we will notice a couple of very obvious errors.

As we can see the first error is that the explosion plays immediately on start and the second error is that the animation is looping.

How to prevent an animation from playing on load?

This one is a simple fix. In our animator component, we can see that the base state of the enemy is in the explosion animation. You can identify the base state as it is coloured orange.

To fix this we add another state (we will call our “Idle”) and select this as the default state. To do this right-click in the animator window (while your enemy is selected), select “create state” and create an “empty” state. You can rename this state in the inspector. Right-click on your new state and select “Set as Layer Default State”

Now we can create a link to our enemy explosion.

We want to create a parameter that tells our animator when to trigger our explosion animation. This parameter can be a trigger and we can call it “Death”. We can turn off the exit time, turn our transition duration to 0 and add our Death trigger to the conditions.

Exit time tells the animator to finish the current animation before transitioning to the next animation and the transition duration is just the point of an animation you start transitioning between two animations. So you get a flow from one animation to the other.

We can take care of the second issue we were having where the animation was looping. Open your explosion animation and uncheck “Loop Time”.

How to set a trigger?

In order to set a trigger, we need a reference to our animator. We can do this in our “ Start function” our enemy. Remember to null check!

Now we have the reference to our animator we can set the trigger using the method Animator.SetTrigger(“TriggerName”). We will place this in our death function.

We have caused an issue with our death function though as we now are triggering our animation and then immediately after we are destroying our enemy.

So from here, we have two options. We can add a delay to our destroy function or we can add another function that destroys the object and trigger this function with an animation event. We will use the animation event.

In order for the function to get triggered in this way, it needs to be a public function.

In order to make an animation event, we need to go into our enemy explosion animation and click the “Add event” button on the frame we want to call our event (which will be our last frame).

If you add the enemy to the scene and select it when you add the event you will get a drop-down to pick the functions you can trigger the animation from.

If you do not put the enemy in the scene you will have to type in the function name.

Now we can test our animation.

That’s all for now.

--

--

Luke Duckett

🎮 First Nations Unity Dev from Wonnarua country 🏞️ | From Player to Lifelong Learner: Crafting Games, Debugging Code, and Embracing New Technology