Unity Dev Blog: Damage VFX Using Animated Sprites in Unity and Triggering then in C# Code

Michael Hatfield
4 min readApr 28, 2022

“HEY! That’s going to leave a bruise!” — Displaying animated damage to the player to indicate just how bad things actually are.

The game is progressing well. I have changed the orientation fro top-to-bottom to side-to-side by basically rotating the camera 90 degrees and swapping the horizontal and vertical input values.

Another major update (in how the game looks) is the animated visual damage you can see when your ship takes damage. It works off of the _lives variable. The code is explained in detail further down but here is a summary:

  • If the player has all 3 lives, there is no visible damage.
  • If the player gets hit once (and therefore has 2 lives left) then the code randomly selects either the right or left wing to display the damage.
  • If the the player gets hit a second time (and is down to only one life) then both of the wings display the fire and smoke animated damage.

Soon, I plan to add a health power-up which will give back a lost life and repair the damage caused by previous hits.

Here is how I implemented the animated damage. First I created animations form the included assets for the damage.

Then I duplicated the animation and place them both as child objects under the player game object in the Hierarchy. The positions are set relative to the player and they move with the player game object.

Next, just disable them in the Inspector. We will turn them on and off in the C# player script. There is no need to make any changes in the Animator component. We want the animations to loop and play as long as the object is visible.

I used a simple switch code block to check for the number of lives. This way I can control exactly what happens at each value. I know I want to add in health power-ups, so by planning ahead I can use this same functionality any time when the number of lives left changes (player gets hit or picks up health)

The first thing that happens when this function is called is that it turns off both right and left engine damage animations. As the code is now, this is not necessary. However, I want it to randomly choose which one gets “damaged” or “fixed” each time. By disabling them both and then randomly turning one on, it works for both scenarios.

If the player has 3 lives, the code makes sure that the damage animations are disabled. If the player gets hit once, losing a life, the switch statement will pick case 2, as that is the number of lives they now have. Then the code will pick a random integer between 1 (inclusive) and 3 (noninclusive) (so really 1 or 2) and then turn on either the right or the left engine damage animation.

If the player only has one life left, then BOTH the left and right engine damage animations will be active. If they get hit again it’s game over. If they pick up a health power-up (to be implemented soon) then they code turns off both and picks one (left or right) to turn back on. This happens instantly so the user never notices. This make it appear that the health power-up fixes one of the engines but not always the same one.

There are several ways you can accomplish this, and I think this makes the game look great. But there is still a lot to work on. Next I will cover a little about PostProcessing in Unity.

See you there!