Using The Animator To Make Enemies Explode After They Die!

Adam Reed
3 min readApr 13, 2021

--

What makes an enemy's death feel good to the player? What will give it that oomph that makes the player feel as if their actions are truly having an impact on their game?

Well, there are lots of examples of how to do this such as screen shake, sound effects, enemy death animations, or in the case of the example today…EXPLOSIONS!

Explosions come in many shapes and sizes and can be used to really show the devastation and destruction that a player is causing as well as the dangers of the environment around them.

The truth though, is that an explosion is oftentimes nothing more than a simple triggered animation…

So let’s learn how to trigger animations!

As instructed in my previous article “How To Import, Set Up, & Animate Your Sprites In Unity” you’ll need to create a sprite animation for your explosion. There are plenty of free assets available online if you don't have your own.

Now you’ll need to create an “Animator Controller” object or use the default one that was automatically generated when the animation you just created was made. Next, attach an “Animator” component to your enemy game object then drag and drop the desired Animator Controller onto the “Controller” slot in the “Animator” component.

Now from within the Animator window which can be found by clicking Window>Animation>Animator within the “window” drop-down menu. Or by simply double-clicking on the desired animator controller.

Now drag and drop your explosion animation into the Animator window and notice a new state appear with the same name as your animation.

You’ll need to create an empty state for your enemy to be in until the explosion is triggered on their death. Right-click on the newly created empty state and select “Set As Layer Default State”.

Then right-click on the empty state and make a new transition to the enemy explosion animation state.

Now create a new parameter of the type ‘Trigger”…

If you click on the transition arrow between the empty state and the animation you’ll be able to open it within the inspector and add the newly created trigger as a parameter to the transition.

Also, uncheck “Has exit Time” as this animation doesn’t need this as a delay when it’s done playing.

And now for the code…

Make a reference to cache the Animator. I named mine “_anim”. Then call that in the start method.

Next, we are going to call on the animator and set the trigger “OnEnemyDeath”, or whatever you named it.

Notice the float value “2.0f” that I put in the parentheses after destroying the game object? That actually delays the destruction of that object for that set amount of seconds. I’ve added this to allow the animation to have enough time to play before being destroyed.

And there you have it! a fully functional enemy death complete with a fancy explosion!!!

[Side Note]

If you don’t want your animation to loop over and over make sure you have “Loop Time” deactivated.

--

--

Adam Reed

Hi, my name is Adam Reed and I am a software engineer specializing in Unity and C# development. Feel free to scroll through and check out some of my work!