Explosions for everyone! How I made the bad guys explode.

How do you get an enemy to explode for you? Not by asking I’ll say that much. Jokes aside, it is a bit more complicated than what I did to make my powerups animate. Lets get on with it!

As with the powerups, we first need to create our animation on our enemy prefab. Alright so what happens now?

Ah so, I need to do a bit more before I get this working correctly haha. Lets go into the Animation Controller then.

Within the Animation Controller of the enemy, I had to first make a dummy state for the default animation or else the enemies would just literally explode as soon as they spawn. I also had to create a Trigger for when the explosion would occur. That Trigger on the left is OnEnemyDeath.

On the transition line from the empty state to the actual animation, I set the condition to OnEnemyDeath triggering. So the animation will play when OnEnemyDeath is triggered in the code. I also made sure to uncheck “Has Exit TIme” so that it would not have to wait for the previous dummy state to finish before starting.

Now within my enemy script there are a few things that I had added in to my enemy death code.

The first being that the speed would be reduced to 0 so the explosion would not move.

Next, after getting a reference to the animator, “.SetTrigger(“TriggerName”)” is used to well, enable a trigger within the animator. This activates the explosion animation.

Third, after also getting a reference to the collider, I set it to disable it as I did not want my Player to get damaged by a lingering hitbox.

Lastly, and probably the most important, is that I added a 2.5f to the Destroy. This 2.5f is the delay in which the code will wait in seconds before performing the destroy.

Now that is definitely a more successful explosion if I do say so myself!

Everyone still there? I hope so as we are getting closer to the end of the base build of the game!

I hope to see you all next post!