Enemy Explosion VFX in Unity!

1. Creating Enemy Explosion Animation

This step is going to be pretty similar to what we have done for PowerUps (*You will need to have a series of Enemy explosion frames ready to be used)

  1. Select Enemy Prefab → go to Window → Animation → Animation
  2. When the Animation Window pops up, click ‘Create’ and ‘save’ it into the Unity folder.
  3. Click on the record button, then drag and drop in your series of Enemy explosion frames into the animation window.

2. Uncheck ‘Loop Time’ in Animation

If you try to play the game, what happens? As soon as the enemies are spawned, they start to explode and explode again continuously, don’t they? Before we figure out how to trigger this animation to start correctly, we can first deal with this looping animation issue.

3. Create a new state that will bridge between ‘Entry’ & ‘Enemy Explosion Animation’

We need to create an empty state that will do nothing when the game started instead of playing the animation instantly. In order to do this,

  1. Select Enemy Prefab → Go to Animator Window.
  2. You will notice there are 3 text boxes included (Any State, Entry, and the animation that you created (In my case, it is ‘Enemy_Destroyed’))
  3. Currently, the Enemy explosion animation is being called out automatically. We need to create a new state (Empty/Null) that will run first, then runs Enemy Explosion.
  • Sequence: Entry → Empty → Enemy_Destroyed
  • This ‘Has Exit Time’ will cause a slight delay when the animation needs to be played. So, in order to play immediately, let’s uncheck this.

4. Parameters in Animator

There are parameters (float, int, trigger, and bool) that we can set in Animator. Then, we can use them to have better control of the animation. In our case, we can use a trigger type parameter that will create a condition and can be called out in our Enemy script.

  1. Go to Parameter window in Animator → Click on ‘+’ → select ‘Trigger’ → Rename it as ‘OnEnemyDeath’

5. In Enemy script

Here is our last step. Let’s go into our Enemy script. When do you think this animation should be triggered? I think the Enemy Explosion animation should play when the enemy was either hit by the laser that the player shot or by the player game object. Then, under OnTriggerEnter2D(), when the enemy collides with the player or the laser, we need to get this animation played BEFORE we destroy the enemy gameobject.

  • IMPORTANT: we need to wait few seconds before we destroy the Enemy in order to play the animation fully. We can add 3 seconds by writing Destroy(this.gameObject, 3f);
  1. The enemy’s speed would reduce down when explodes.
  2. The enemy can no longer collide with the player when explodes.
  1. Create a bool variable _isEnemyDying which will be false at start.
  2. Add if statement if (_isEnemyDying == false) so that OnTriggerEnter2D() will run only when it is false.
  3. We will turn _isEnemyDying = true when the enemy collides with either the laser or the player.
  4. We will also write _enemySpeed = 2f; to reduce the enemy’s speed.

--

--

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
Du Young Yoon

Du Young Yoon

XR Unity Developer / Designer / Architect