There are multiple ways to add sprite VFX to your game. In this article, we’ll cover how to enable VFX by converting the effect into a gameObject. Then, we’ll enable the gameObject to play the animation at the appropriate time.
The Damage VFX
In my 2D Space Shooter game, I want an effect for the player’s ship to show the number of lives the player has lost. I have a fire sprite animation that I’ll use to represent the player’s damaged engine when the player loses a life.
To set up the animation, I’ll take the first frame of the animation, drag it into the Hierarchy and call it EngineFireR. Then, we’ll open up the Animation window, and with the EngineFireR gameObject selected, we’ll create an animation called EngineFire_anim. Next, select the sprites for the animation and drop them into the dope sheet. When you hit the play button in the Animation window, you should see the animation play.
Now, we can duplicate the EngineFireR object and rename it to EngineFireL. We’ll place them right on top of the player’s engines.
However, we don’t want to keep the animation enabled all the time, so we’ll set them to be inactive initially. In PlayerScript, we need to tell the player when to enable the VFX we just made.
Luckily, this is pretty simple. We’ll create references to both gameObjects and tell them when to turn on. We want the right engine effect to activate when the player has only 2 lives remaining, and we want the left engine effect to activate when the player has only 1 life remaining.
After creating Serialized references for the _rightEnginePrefab and _leftEnginePrefab, we’ll drag the VFX gameObjects into the script. Now, when the player takes damage, the player’s engines will show engine failure VFX!