Enemy Explosion VFX in Unity!

This is going to be another exciting topic to discuss today — VFX (Visual Effects)! It is going to be similar to how we created PowerUp Animations, but more in-depth. Ok, let’s get started!

1. Creating Enemy Explosion Animation

  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.

4. It’s looking amazing when test-played!

2. Uncheck ‘Loop Time’ in Animation

In order to stop the animation looping, we need to click on the Enemy Explosion animation that we created above. In the Inspector window, you will see a text called ‘Loop Time’. We need to uncheck this.

Now, the animation will only play once.

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

  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

4. Click on the arrow between Empty and Enemy_Destroyed. Then, in the Inspector window, uncheck ‘Has Exit Time’.

  • 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

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

2. Now, let’s click on the transition arrow between Empty and Enemy_Destroyed again. Then, in the Inspector window, under Conditions, click on ‘+’ sign → add ‘OnEnemyDeath’.

Awesome! Now, we just need to work on the script that will call this ‘OnEnemyDeath’ parameter out.

5. In Enemy script

In order to start the animation, we need to get “OnEnemyDeath” condition called out. That means we need to get the Animator component at the start.

Now, we can say _enemyVFX.SetTrigger(“OnEnemyDeath”) to get the animation played BEFORE the Enemy gameobject is being destroyed.

  • 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);

Let’s check out if it’s working properly.

Hmmm… We shot the enemy and the animation is playing properly, HOWEVER, the enemy can still move forward at the same speed and collides with the player that causes the player to lose 1 health point.

I think it would make much more sense if

  1. The enemy’s speed would reduce down when explodes.
  2. The enemy can no longer collide with the player when explodes.

In order to achieve 2 statements above, we need to

  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.

Let’s see if it’s working better.

Cool! When the enemy collides with the laser, the speed is reduced & no longer collides with the player!

Thank you so much for reading!

XR Unity Developer / Designer / Architect