Creating Enemy Explosions

Andrew Crippen
3 min readJun 3, 2021

I had already created enemy explosions from a 3d particle prefab earlier in development of this Unity space shooter game but I’m going to show how to animate and trigger 2D sprite explosions for the enemy instead.

First double click and open the Enemy prefab in the Project tab.

Now go to the Animation tab and hit the Create button which automatically adds an Animator component and creates an animation controller for the Enemy GameObject. Create a new folder inside of the Animations folder named Enemy and then name the new animation file Enemy_Destroyed_anim.anim.

Select the 1st Enemy_Explode_Sequence sprite, hold shift and then click the last sprite, this will select them all, now drag all of the sprites into the animation window.

Animation window after dragging the sprites into it.

Locate the animation file by going to the Animator tab, click the Enemy_Destroyed_anim state and then click it in the Motion field in the inspector, this highlights it in the Project tab, click it there and then uncheck Loop Time in the inspector to disable the animation looping.

In the Animator window right click and Create State>Empty, right click this new state and select Set as Layer Default State.

Click the Parameters tab in the upper left of the Animator window and click the + icon to create a new Trigger named OnEnemyDeath.

Right click it again and select Make Transition and select the Enemy_Destroyed_anim state. Hit the + under Conditions to add the OnEnemyDeath trigger.

Click this transition arrow and uncheck Has Exit Time, this will prevent it from lagging before it transitions to the explosion.

Now for the code part…

In the Enemy script add a private variable for the Animator and the BoxCollider2D

Get a reference to them in the Start() method.

Now in my DamageRoutine() coroutine I disable the collider after it is hit by a laser or player. I then nullcheck the animator, trigger the explosion animation with SetTrigger and set the speed to 0.

--

--

Andrew Crippen

Unity Developer/C# Programmer for VR, 2d,3d Games and other Immersive Interactive Experiences