Loving the smell of napalm in the morning!
Creating Enemy Explosions
Today, we make our enemies go boom! Let’s look at adding explosion animations to our enemy objects. To get started we’ll open our Enemy prefab and, with our enemy object selected, open our animation window. Clicking on the create button will let us create our animation and a controller for it. Then we’ll drag the sprites for our animation into the dope sheet.
Alright! let’s check on how that looks in Unity. Oh! no! It looks like our enemies are now auto exploding and exploding over and over!
First lets fix the looping by selecting our newly created anim file in the inspector and unchecking the loop time option.
Next let’s see about making the explosion animation play only when we tell it too. We’ll need to open our animator by double clicking the controller file, then we can add a new empty state by right clicking and selecting “Create State > Empty”. Next we can create a new transition from our empty state to the explosion animation by right clicking on the empty state, selecting “Create Transition”, and then dragging the transition to the animation node.
now that wee have a transition we need a way to control it so that it only changes state to our animation when we tell it to. we can do this by creating a parameter and applying it to our transition. First we click on the “Parameters” tab, and then we click on the plus symbol to create a new parameter and select Trigger. We’ll call it “OnEnemyDeath.”
Next we’ll apply the trigger we just created by clicking on our transition line and, in the conditions section in our properties inspector, we add a new condition by clicking the plus symbol. If it’s not auto selected, we can select our “OnEnemyDeath” trigger in the dropdown.
With our trigger setup we can now connect it to our c# code where the enemy is being destroyed. Let’s start by creating a field to hold our animator component in the Enemy class, initializing it via GetComponent in our start method and null checking it to make sure it’s there.
Now in our OnTriggerEnter2D method, where we destroy our enemy, let’s use the animator to Set our trigger and play our animation before we destroy the enemy object.
Ok! Let’s check our progress in the Unity game window! Wait our animation doesn’t seem to be playing! Well, that’s not quite true. What’s happening is that our game object is being destroyed, along with the animation, before it has time to play!
How can we fix this? Well it turns out that the Destroy method can take in a parameter for a delay before the object is destroyed! We can take a look at how long the animation clip is in our animator and then delay the destruction of our enemy for that length of time, which in our case is about 2.64 seconds. We will also want to grab a reference to the collider on the enemy object and disable it so that we don’t take damage from the enemy if we run into it while the animation is playing.
Alright! now our enemies explode properly but the animation sometimes seems delayed. This happens because the animator wants to complete the running state before it transitions to a new state and if we trigger that animation at the beginning or in the middle of one of these cycles it takes extra time before it transitions to the animation. we can fix this by disabling the “Exit Time” option on the transition in the animator.
And now our enemies explode promptly when we damage them!
Next Time!
Today we learned how to add some extra visual flair to our game by adding an exploding animation on our enemy death! Next time, we’ll look at adding damage effects to our player! If you enjoyed this article, or want to come along with me as I progress on my journey, follow me at gamedevchris.medium.com.