Journal 24 — Enemy Explosions in Unity

Chris Nielsen
5 min readMay 14, 2021

--

This example is focused on the implementation of the explosion animation for the enemies, and the updates in Unity and the behavior in C#. Our goals are to:

— Add an explosion animation to the enemy ships on their destruction

— Figure out how to play the animation in C#

— Tweak the settings and code to make sure the behavior makes sense

In order to add an animation, we start with opening the enemy prefab.

Unity prefab selection in the Inspector

Similar to the previous examples on adding power up animations, we make sure the enemy prefab is highlighted and go to the Animation window, create a new animation clip, hit the record button, drag the animation sequence to the animation window, and save the prefab.

Unity Animation window, hit record and drag in sequence of animation sprites
Unity Animation window, sprite animation is added, so stop the recording
Unity Animation window, test the animation clip
Unity Prefab editor, need to save and exit to save the new animation

Now in our assets folder we see the newly created animation clip, along with the Enemy Animator Controller.

Unity assets, new animation clip and Animator Controller

If you try running the game to see the new effects, you will find that the destroyed animation is automatically played on each prefab. This is because in the Animator Controller, the destroyed animation is the “default state”.

Unity Playmode, destroyed animations just automatically play

To fix this, we create a new “Empty” state, and set that layer as the default state.

Unity Animator Controller, fix the default state

Next we need to create a transition from the default state to the destroyed animation.

Unity Animator Controller, add a transition

After selecting the newly created transition, we want to add a “Trigger” parameter to access from C# to call the destroyed animation at the right time.

Unity Animator Controller, trigger for destroyed animation

Now it’s time to add the animation in C#. In the enemy C# script, we add a reference to the Animator component.

Unity C#, add Animator reference

As a first trial, we will try adding in the animation trigger just before the enemy game objects are destroyed.

Unity C#, add a call to trigger the destroyed animation

To see if this works correctly, we try running the game. However, no animations are showing up!

Unity Playmode, no animations!! ##@!%^

The reason is that as soon as the animation is called, the game object is immediately destroyed. So we want to add a short delay for around the length of the animation clip.

Unity C#, add a delay to the Destroy command

Now when we test this, we can see that there is a delay in the lasers hitting the enemies before the animation plays.

Unity Playmode, animations starting to make sense

The reason for this delay is that the destroyed animation currently has a setting called “Has Exit Time”, which means the animation trigger will not start until the default state has finished its cycle. Let’s deselect this, and also go ahead and 0 out the Transition duration, as there is no transition time required.

Unity Animator Controller, adjust the Exit Time settings

Now when we try these updates, we can see the animation looks more accurate. However, as you can see, even after the enemy is destroyed and the animation is playing, the enemy is still moving and is still colliding with the player, causing damage.

Unity Playmode, mostly there but player still damage by destroyed enemy!

To address this we can simply stop the enemy speed.

Unity C#, stop the enemy movement when they get destroyed

And here are the results of this implementation. The animation is playing correctly and looks accurate.

Unity Playmode, animation and behavior looking pretty good

After going through this exercise, I had some additional thoughts on how to tweak the look and feel. One of the settings is related to the destroyed animation and how this looks when overlapping with the player. Currently the enemy and player are on the same sorting layer.

Unity Sprite Renderer sorting and order

As you can see during Playmode, there are some apparent conflicts in the sprite rendering between the player and the animation.

Unity Playmode, player and enemy sprite conflicts

To address this, let’s try updating the enemy prefab sorting layer and make it 1 instead of 0, so the destroyed animation is rendered above the player. Don’t forget to save this edit to the prefab.

Unity enemy prefab, change the Sprite Renderer order

The final modification has to do with allowing the enemies to still keep moving while they are being destroyed. However, we don’t want a collision to still occur with the player and cause damage. To address this, we can simply disable the 2D collider on the enemy once it’s been hit by a laser.

Unity C#, disable collider so enemies keep moving but don’t damage player

And here are the results!

Unity Playmode, final cleaned up animation and behavior

Thank you for your time!

--

--

Chris Nielsen
Chris Nielsen

Written by Chris Nielsen

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.

No responses yet