2D Galaxy Shooter — Making the Enemy Explode using VFX!

Marcus Cohee
3 min readAug 24, 2021

Hello all!

Today, I will go over how to make enemies explode using Visual Effects (VFX)! This will go into animations, the animator, and calling those components using GetComponent!

Note: This requires a sequence of sprites to animate our Enemy.

Satisfying…

Adding an Animation to the Enemy Prefab

  1. Double click the Enemy from the prefabs folder.
  2. Open the Animation window.
  3. Create an Animation called “Enemy_Destroyed_anim”.
  4. Hit the record button.
  5. Drag in the sprites needed for your animation.
  6. Test them out by pressing the play button and watch your enemy explode!

Setting Up the Animator

This section will help us out later. If we leave the animation the way it is now, as the enemy is going down, they will constantly explode. We need a way to trigger a parameter on death to explode.

Animator window
  1. Open the Animator window with Enemy selected.
  2. Right click the empty black area, Create State, then Empty.
  3. Name the new block “Empty”.
  4. Right click the Entry block and click “Set StateMachine Default State”.
  5. With the arrow, click the Empty block.
  6. Right click the Empty block and click “Set Transition”.
  7. With the arrow, click the Enemy_Destroyed_anim block.
  8. Now, on the left under Parameters, hit the + button.
  9. Create a Trigger parameter and make sure its unchecked.
  10. Select the arrow between the Empty and Enemy_Destroyed_anim.
  11. In the Inspector, look where it says Conditions.
  12. In the field below that, hit the + button. Since OnEnemyDeath is the only parameter, it will automatically assign it.
  13. Above that where it says “Has Exit Time”, uncheck that.
  14. Drop down the arrow right below that and make all values 0. By doing this, it will start the animation as soon as we call OnEnemyDeath.

Animation Behavior

All this will occur in the Enemy script.

  1. Open the Enemy script
  2. Add variable = [SerializeField] private Animator _onEnemyDeath;
  3. In start, lets NULL check when we grab the animator component:

4. Head down to the OnTriggerEnter method and enter what’s below:

So, if the Enemy is triggered by the Player or Laser, then these will run:

If hit by Player:

  1. Damage player.
  2. Set the Enemy speed to 0.
  3. Call the parameter and start the animation.
  4. Start the coroutine that says after 0.4 seconds, turn off the Box Collider 2D component.
  5. Then after 2.0 seconds, destroy the enemy.

If hit by Laser:

  1. Give the Player 10 points.
  2. Set the Enemy speed to 0.
  3. Call the parameter and start the animation.
  4. Start the coroutine that says after 0.4 seconds, turn off the Box Collider 2D component.
  5. Then after 2.0 seconds, destroy the enemy.
  • Notes:
    - My animation runs about 2 seconds, that’s why I destroy the enemy after 2 seconds.
    - The reason I turn off the Box Collider 2D after 0.4 seconds is because the enemy looks mostly vaporized by that point. I do this so when I kill an enemy, it doesn’t soak up lasers till it dies.

Test it out in play mode, you will be most satisfied watching your enemies blow up by your hands!

This has been how to add exploding animations to the enemy! Next, I will add an exploding asteroid that holds off enemy spawns till blows up!

Thank you for your time!

--

--

Marcus Cohee

I am starting Unity and learning the ropes as I go! Let’s take this journey together!