2D Galaxy Shooter: Player Damage VFX!

Hello everyone! Today is another exciting day. I implemented a damage VFX visualizer for when the player is hurt!

The objective is to show player damage through the stages of its lives. Let’s go on!

I used a sprite ready to be animated that resembles a fireball.

I created a new animation clip, dragged in the frames, and voila! It’s alive! Ready to be used.

We have to remember that the player has three lives. When the player has three, we do nothing. Its hull is in mint condition.

If the player gets hit, then reduce the lives to two, and show damage.

If the player gets hit again, then reduce the lives to one, and show more damage.

This is where we decide that we need two game objects of the fireball animation. I name the game objects to Engine1Sprite and Engine2Sprite. I could have left the “Sprite” part out… oh well lol.

I placed them in the Player object because we want the animation to move along with the player. I also disabled them because we are going to activate them in code when the player is damaged.

Now that we have this all set in Unity, we need to code in the logic. Let’s open up the Player script and get to work!

We need a reference for the two engines. I initialize and declare an array of GameObjects:

[SerializeField] private GameObjects[] _enginePrefabs = null;

I add the SerializeField attribute so we can see this field in the inspector.

Next, let’s head back into Unity and drag the engines into our new field. This way, we now have our reference all set.

Back to the Player script, we now need to add logic, after, where the player lives are taken.

public void Damage(int damageAmount)
{
if (_isShieldActive)
{
_isShieldActive = false;
_shieldPrefab.SetActive(false);
return;
}
_lives -= damageAmount; //This is where we want to add the logic if (_lives <= 0)
{
_isDead = true;
_spawnManager.StopSpawning();
_lives = 0;
Destroy(gameObject);
}
}

We need to use an if-else statement to check how many lives the player has.

if (_lives == 2)
_enginePrefabs[0].SetActive(true);
else if (_lives == 1)
_enginePrefabs[1].SetActive(true);

If the condition is true, we need to activate the engine game objects.

public void Damage(int damageAmount)
{
if (_isShieldActive)
{
_isShieldActive = false;
_shieldPrefab.SetActive(false);
return;
}
_lives -= damageAmount;if (_lives == 2)
_enginePrefabs[0].SetActive(true);
else if (_lives == 1)
_enginePrefabs[1].SetActive(true);
if (_lives <= 0)
{
_isDead = true;
_spawnManager.StopSpawning();
_lives = 0;
Destroy(gameObject);
}
}

With the implementation of this logic, we now have a working damage effect!

I would have thought that such a feature would have been difficult to implement. But, I was wrong!

It’s a simple and effective feature to make the game visually pleasing.

That is all for today! Thank you for reading!

Hello everyone, My name is Gabriel Perez, I am a Unity Developer and a creator who is always learning and experimenting.

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