Journal 24 — Enemy Explosions in Unity
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.
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.
Now in our assets folder we see the newly created animation clip, along with the Enemy 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”.
To fix this, we create a new “Empty” state, and set that layer as the default state.
Next we need to create a transition from the default state to the destroyed animation.
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.
Now it’s time to add the animation in C#. In the enemy C# script, we add a reference to the Animator component.
As a first trial, we will try adding in the animation trigger just before the enemy game objects are destroyed.
To see if this works correctly, we try running the game. However, no animations are showing up!
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.
Now when we test this, we can see that there is a delay in the lasers hitting the enemies before the animation plays.
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.
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.
To address this we can simply stop the enemy speed.
And here are the results of this implementation. The animation is playing correctly and looks accurate.
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.
As you can see during Playmode, there are some apparent conflicts in the sprite rendering between the player and the animation.
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.
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.
And here are the results!
Thank you for your time!