Making Things Go Boom! Adding Enemy Explosions in Unity

Currently when we hit an enemy with our Lasers they simply disappear. Let’s utilize our knowledge of animations and some really nice assets to make those enemies explode instead.

We start by double clicking on our enemy prefab in our project view. Then from our Animation tab we create a new Anim called “Enemy_Destroyed_anim”.

Press record and grab all our animation sprites and drag them into the animation dope sheet.

Now our prefab will automatically explode as it moves down the the screen. We have to break this state down so that it only begins the animation process after it has been hit. We click on the Animation Controller and turn off its default loop. This will only ensure that the enemy explodes once.

By double clicking on our “Enemy_Destroyed_anim” controller we see the current state flow of our enemy’s animation. It runs the animation by default. We want to add a new Empty state between the Entry point and the actual animation.

We want to add a new Empty state between the Entry point and the actual animation. We make this the default state. We can then create a new transition between the Empty and the “Enemy_Destroyed_anim” and with this new transition we can create a new parameter for when we choose for it to be played.

We have 4 parameter types that we can choose from Float, Int, Bool and Trigger. For this animation we want to choose the Trigger since we want the animation to trigger when it is his by the laser of our player. In this case I create a onEnemyDeath trigger.

I make sure that it is turned off by default. We then want to turn off “has exit time” in the inspector and set its condition to onEnemyDeath.

Next we are back to scripting our tigger behavior in our Enemy script. We create a handle to the Animator get its component in our Start Method() check for NULL and then call it when the collider is triggered using the handle (_enemyExplosion.SetTrigger(“onEnemyDeath”) Through trial an error we also decide to stop the enemy’s movement and delay its actual destruction so that the animation plays for the length of time we want.

Its seems complex but its a very repeatable pattern that once fully understood really highlights the power of Unity’s animation engine. Where else might we use this…stay tuned and I’ll show you! Adios!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store