120 Days to Master Unity (Day 10)

Brian Perry
4 min readMay 27, 2021

Damage VFX using Animated Sprites in Unity

In this article, I will discuss creating damage in 2D sprite based games. In Unity, its easy to create effects. In my case, all I needed to do was; drag in a frame from my damage animation, copy and paste the entire animation into my dope sheet for the game object I wanted animated, and add a little logic to turn that effect on and off at the right times.

Using my access to the GameDevHQ library, I first imported the very first frame of the animation which would represent the player’s ship being damaged.

Its important that the frame is dragged into the player game object. This is important because like the shield or any other effect that will be created on the player, the new frame which is now a game object, must be a child of the player game object. This causes the animation to stay attached to the player game object as though it were a piece of the player object. After that, it is a good idea to rename the new game object. I wanted to create two engine damage animations on the left and right side so I named my engine damage animations leftEngineDamage and rightEngineDamage. For the next step, drag in the animation you wish to use into your animator dope sheet. Before you can do this, the game object you want to animate must be selected and the animation window needs to be opened. To open the animation window, go to Window menu item on the Unity menu and select Animation -> Animation. This will open the Animation floating window which can be docked next to the game window.

Opening the animation window

Once the Animation window is docked, drag and drop a frame for the animation onto the player game object to create a child game object. Next, go to where the animation frames are stored and select all of the animation frames that are part of the animation. Once all of the frames you want are selected, drag all of the frames into the animation dope sheet in the animation window.

Making a player damage game object

Make sure before dragging the animation into the dope sheet you have an animator component for your animation. If you do not have an animation component, you will not be able to drag the animation frames into your animation game object. Once the animation frames are dragged in successfully, you now have a fully functional animation!

Engine damage animation

The next step is to create the logic for what happens when the player’s ship is damaged. The first step of creating this effect is to disable the object you just created.

You only want this object to be seen when the ship is damaged so you only want to activate this object when the ship becomes damaged.

Now the last thing that needs to be done is create the logic so that the engine damage animation plays as the player is damaged during the game. First, create a way for your player game script to be aware of your new animations. This can be done by creating two game objects. Create one game object variable for the left engine damage animation and another game object for the right engine damage animation.

Make sure to use [SerializeField] so that the variable shows on the inspector window when the Player game object is selected.

Select the Player game object and now your animation variables should be shown. Drag and drop you left engine damage animations into the variable slots. After this is done, there is a way to use the variables in your player script.

Show damage when player is damaged

There is already a block of logic that determines what happens when the player’s ship in my game is damaged. In my case then, I just have to add a function in the spot where the player is damaged to trigger the animation that shows the player getting damaged.

I use a switch statement to show the player being damaged. When the player takes 1 point of damage, the left engine damage animation plays. When the player takes 2 points of damage, the right engine damage animation plays.

After this logic is added, the ship will show damage as the player takes damage in the game.

--

--