Damage VFX using Animated Sprites in Unity

Objective: After a collision with enemy lasers, add some nice Damage VFX in our spaceship.

Damage VFX on the Player

Requisites: A Gameobject player, 2 gameobjects with animated sprites showing the spaceship damage and laser-player script communication.

The first step is pretty simple, create 2 empty gameobjects inside the player and attach them an Animator component with their respective animations

Place your disabled Gameobject animations under your player and they will follow the player movement even if they are deactivated

Now we open our projectile script and make use of the OnTriggerEnter2D() Unity Event

OnTriggerEnter2D gives us the information of the GameObject that collided with us(laser)

Step by step:

1- We check if we collided with an object with the tag “Player”.

2- We get the player reference finding the GameObject Player and getting the player script component Player

Remember to declare the variable to store the player reference first

3-Null check, if we succesfully get the player script, we call the Damage function in our player script. We’ll check the implementation later.

4-We destroy the laser projectile.

After saving and going back into Unity, open the Player script and get into the damage function implementation. In this function, when we receive damage we substract a life with _lives — ;. We also call another method called CheckEnginesVisual(). Here is where the magic happens. Let´s get into it.

For this we will need the engines GameObject references:

GameObject Engines reference

Both engines references must be dragged to the player in the Unity Editor to work!

Remember: Good practices in programming say that we should always keep every variable private unless the value is going to be accessed or modified by an external script.

CheckEnginesVisual implementation.

If the player gets damage, we activate and deactivate the engines whenever is needed.

And that would be it for our Damage VFX implementation!

Passionate Unity developer always looking for new challenges!

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