My journey becoming a Unity game developer: Damage VFX using Animated Sprites in Unity.
Objective: Create visual effects showing the Player being damaged. Then when the Player runs out of lives, have a big explosion signaling the end of game for the Player. We will use sprites and animations to show these damage effects to the Player.
First thing we will do before setting up the damage effects to the Player is add a thruster with animation to the Player’s ship. This will give the appearance of the ship being active in space.
Create a Thruster game object as a child of the Player object, and Reset its Transform. Position the Thruster at the bottom of the ship, and set the Sorting Layer to Foreground with Order in Layer set to zero to be behind the ship. With Thruster object selected, go into the Animation window and create a new animation name Thruster which will automatically create an Animator component. Drag your Thruster sprites into the Dopesheet and play.
Now to add damage VFX to the player, drag a Player_Hurt engine sprite into the Player object as a child. Rename it Right or Left Engine Failure, Reset its Transform, and position the sprite on one side of the ship’s wings edge. Important to set the Sorting Layer to Foreground, and Order in Layer to 1 as we want the engine failure effect to be on top of the wing’s tip being very clear to the viewer. Create a new animation name Player_Hurt and drag in all of the Player_Hurt sprites into the Dopesheet. Press Play to see the animation playing, and if everything looks fine, duplicate this engine failure object and name it the opposite name of the 1st failure object. We will use our script to activate these failure objects, therefore turn off both objects in their Inspectors.
In the PlayerBehavior script, create 2 new variables that have SerializeField with Type GameObject name _rightEngineFailure and _leftEngineFailure. Inside the Damage() method, check IF _lives are equal to 2, and get the _left or _right engine failure variable. Use the SetActive() method to turn it on by setting it to True. Check IF _lives is equal to 1, get the other engine failure variable and SetActive() it to True as well.
Select the Player object and drag the Left and Right Engine Failure objects into their respective slots under the PlayerBehavior script component. Play the game, damage your player’s ship, and watch the engine failure animations play after being damaged each time.
To add a VFX effects to the Player’s ship, go back to the PlayerBehavior script. Create a new variable with a SerializeField Type GameObject name _playerExplosionPrefab. Inside the Damage() method under IF _lives is less than 1, Instantiate the _playerExplosionPrefab at the Player’s position with no rotation right before Destroying the Player object.
Back in the editor, select the Explosion prefab and Create a Prefab Variant name Player_Explosion. This prefab will be a child of the Explosion prefab meaning any changes made to the Explosion prefab will automatically be made to the Player_Explosion prefab because Explosion is the parent. Any changes made to the Player_Explosion prefab itself will not affect the Explosion prefab as a child doesn’t have authority over their parent.
Reset its Transform, Sorting Layer equals Foreground with Order in Layer equals 1 to be in front of the ship, and change the color to something else that will stand out from the enemies explosions.
Play the game and notice when the Player loses all their lives, the explosion animation plays but with a very red color representing the Player exploding.
If you want the enemies to have thrusters on their ships, duplicate the Player’s thruster object and drag the duplicate onto the Enemy prefab. Reset Transform, and Rotate 180 degrees on Z-axis.
Then in EnemyBehavior script, create Type GameObject variable with a SerializeField name _thruster. Where Player receives damage from Player and Laser add in _thruster.SetActive(false) under SetTrigger(“OnPlayerDeath”) to turn off the thruster object.
In the editor, open the Enemy prefab, go to the EnemyBehavior script component and drag the Thruster object into the Thruster’s variable slot.