Creating Enemy Explosions

Objective: Make an enemy explosion when the enemy is destroyed.

Currently when we destroy an enemy it just disappears from the scene. Let’s add some animation to this.

First we have to open the enemy prefab, go to the animation windows, create an animation and add the respective sprites for the animation. In this case we have to uncheck the loop time in the controller because the animation has to run just once.

Then Open the animation controller, create an empty state and set it as default. We do this to avoid running the explosion animation when the game starts. After that we’re gonna create a parameter of type trigger called OnEnemyDeath to trigger the animation through script.

In the enemy script we have to declare a variable to get the animator reference. Then create an Explode function where we are gonna use the method SetTrigger from the animator to trigger the explosion animation, set the speed to 0, disable the collider and destroy the object.

That Explode function has to be called in each case where the enemy is destroyed.

Nice! We are good to go. When we run the game we can notice the enemy explode when it is destroyed by a laser or colliding with the player.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rusben Guzman

Rusben Guzman

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.