Creating Enemy Explosions- Game Dev Series 23

Objective: Create explosion effect & function to enemy.

Previous: Reloading Now

Let’s make some visual effect to our enemy when destroyed by player. Open the Animation window from the top drop down menu >>.
Open the prefab of enemy, create an animation in window.

When we create an animation, an animator will also created. Inside the folder in , there will have be an animator called (which is a bug of Unity 2019, it should be the same name as animation). Double click and it should open the Animator window.

By default, it will automatically connect to animation just created. To prevent enemy got explosion when game starts, we can create an empty by right click in the > > . And right click to it, set it as default.

And now we need add a condition to trigger the animation.

On the top left of , there are 2 tabs. Click on where we can create parameter as a condition to start the animation. Click the add mark, choose trigger and name it .

Then we can right click on the Empty layer> Make Transition, then drag it to connect with Enemy_Destroy_anim.

Click the transition arrow, we can adjust the setting of it.
Uncheck , click the drop down and set to , then add a condition and set the to it.

Now we have set the animation condition, we can trigger it in enemy script.

In the script, set a variable of animator and get this component in .

There are 2 situation that enemy would be destroyed: collide with player and laser. These 2 are all need to trigger the animation. Add the function in these statement.

I added 2 more lines under the animator trigger.
One is turn off the collider to prevent trigger again with player, and one is to decrease the speed of enemy and the animation would be more realistic.
And also set a delay to destroy enemy gameobject, it would let the animation runs completely.

Now we can Play it and enjoy the animation.

A passionate gamer whose goal is to work in video game development.