The Art of Blowing Things Up: Explosions (Game Dev Day 24)

Objective: Add an on death animation to our enemy ship

Finalized animation.

First thing is to create the animation itself. Create a new animation from the Animation window and give it a name (I used Enemy_Destroyed_Anim).

Now we drag the frames into the Animation window and compile the animation. Unity will handle all the heavy lifting for this, it’s just drag and drop.

Once our animation exists, we need to decide when it should play. In this case, we want the animation to play when an enemy ship is destroyed.

Let’s attach our new animation to our Enemy prefab. We do this by attaching the animation controller assigned to that animation. By default, it should be named the same as the animation, minus the _anim.

Ensure you are dragging the controller in. Trying to drag the actual animation won’t work.

Open the Animator window (this is different than the Animation window) . If you don’t see it anywhere on your workstation:

Window>Animation>Animator will bring it up.

Here we will differentiate when to play the Enemy Death animation.

We need to create a new parameter using this window.

Use the + sign drop down and select “Trigger”.

I named this trigger “OnEnemyDeath”.

Next in the neighboring window right click>Create State>Empty

Make a new animation state to hold our enemy death animation.

Once you have it created, left click on it and drag your Enemy_Destroyed_anim into the “Motion” category in the inspector.

By default, the animator will start on the “Entry” option. Drag a transition arrow to the “Empty” orange box. This will stop our death animation from playing all the time.

We only want it to play when the enemy game object is being destroyed.

Next, drag another transition arrow from the orange “Empty” box to your Enemy Destroyed state.

Now we set the rules for when the game should stop playing the empty animation (which does nothing) and should play the death animation.

Left click on the arrow between the Empty and Death states, and at the bottom look as the “Conditions” tab. We will be adding the parameter we made earlier.

Just click the + sign and it will add your Parameter.

While we are here, make sure the “Has Exit Time” option is turned OFF. If you don’t do this, there will be a pause before the animation plays and it will look very strange.

Great! Now all that if left is coding the logic in our Enemy.cs script.

In our Enemy.cs script, we need to add a new variable.

Put this in Public Class Enemy: Monobehavior.

In void start, we need to get the animator component that is attached to our enemy game object. Because this script is also attached to the game object, we can just call the component directly.

Place in void start

Finally, lets add the trigger to the enemy death states. The enemy ships can be destroyed by a laser OR by colliding with the player, make sure both situations use the animation.

Most of this we created previously, but we added the animations and changed a few things.

First thing we did was add the animation trigger to the OnEnemyDeath parameter.

Second, when this method is called the enemy movespeed will be reduced to 0, so that the player cannot be hit by the collider still moving during the animation. You could also set this to something close to 0, as it may look a little more natural (I would try 0.5).

Lastly, we added a delay of 2.8 seconds to the Destroy game object logic. This is so the animation has time to fully play before the enemy object is removed.

You can check the animation length of whatever your animation is in the Animation window.

Now our enemy ships will explode when destroyed.

Explosions in-game.

NOTE: During the enemy death animation, the collider is still active.

This means it can block your shots, or if you fly into it, it WILL damage you.

You can add code referring to the 2D collider in your Enemy.cs script to disable the collider during this time.

I decided to leave it in, because it made more sense from a gameplay standpoint . An exploding ship hitting you would hurt, and would still be capable of blocking lasers.

Unity Developer