Damage VFX using Animated Sprites in Unity

Enemy, Enemy Asteroid, Player, and Level Start Asteroid

The Enemy Explosion works Great for the Enemy Ship. This requires several additional parameters on the Damageable Behavior just to play the animation for the Enemy. When the Power ups get destroyed causes several warnings because they have an Animator but no Is Dead Animator Parameter.

To fix this I am going to change over to a prefab that will get instantiated if it is assigned in the inspector.

Damageable Behavior

The First thing I did was removed all of the code I just added for the Enemy explosion. I Added a explosion VFX Game Object that can be set in the inspector. In the Damage Method if the exlosion VFX Game Object is set that I Spawn it in the Game. I then Destroy the Game Object that the Damageable is attached to.

Creating the Explosion VFX’s

I have 2 different explosion VFX’s the enemy ship explosion and a generic explosion.

Generic Explosion

I created a Explosion Game Object.

I Created the Animation from the Sprites.

I Created this as a Prefab.

Enemy Explosion

I created a Animator Override Controller called Enemy Ship Explosion and A Animtion called Enemy Ship Explosion Anim. An Animator Override Controller allows you to use an existing Animation Controller to set up all of the logic, parameters, and structure allowing you to Use specific animations. You do not have to replace all of the original animations in the controller, if you don’t replace an animation it will play the original.

In my case I will override the Explosion Controller with the Enemy Ship Explosion.

I then Modified The Explosion Prefab to use the Enemy Ship Explosion Animator. I created this as a Prefab Variant.

Using the New Explosion VFXs

I then set the Player, Enemy, and Enemy Asteroid Prefabs to use the correct Explosion VFXs.

Now when The ships are destroyed they play the Explosion animation.

Animation Fixes

There are several problems with the new explosion.

  • They stay in the Game forever.
  • They Repeat over and over, It should just play the animation once. Unity Defaults to loop.
  • The Enemy Ship Explosion animation is an Empty Animation.

Destroy Explosion

I created an Explosion Behavior. I want the animation to have time to Play before the Game Object gets Destroyed. I have a float called Destroy Delay Time and set it to 2.8f, this is the time that the explosion takes to play. In the Start Method I Destroy the Game Object after the delay time.

I added an Explosion Component to the Explosion Prefab. Since the Enemy Ship Explosion is a Variant of the Explosion Prefab the Explosion Component is automatically added to it.

I changed the Delay Time to show that the Explosion VFX does destroy itself.

Fix the Lopping

To stop the Explosion From Looping I changed Loop Time For the Animation to False.

Fixing the Enemy Ship Explosion Animation

I draged the Enemy Ship Explosion Prefab into the Scene, Selected the Animation and added the Sprites to the Animation.

The Explosions Working in the Game.

Another Object that Explodes.

I am going to add an Asteroid that rotates. When the Player Destroys it It will start the Spawning of the Enemies and Prefabs.

I started off by creating the Asteroid Game Object. I added a Circle Collider, A Ridged Body and Added the Damageable Components. I Also Changed the Name to Level Start Asteroid.

I set the Damageable Parameters so the Player can Destroy it when they shoot it, when it gets Destroyed the Explosion VFX is played, and the Level Start Asteroid Destroyed Game Event will be Fired when it is destroyed.

Rotate Behavior

I used the Rotate method on the transform Component to rotate the Game Object. I took the rotation axis and multiplied it by the speed multiplied by Time.deltaTime to make it rotate in the Update Method.

Start Spawning

I removed the Start Spawning Method call from the Spawn Manager. This will make the Game not spawn anything.

Making the Game Start Spawning when the Level Start Asteroid is destroyed is as easy as adding a Game Event Listener to the Spawn Manager and telling the Listener to Start Spawning When the Level Start Asteroid Game Event happens.

Excited about changing my hobby into a new carer with GameDevHQ course.