Adventures in GameDev with GameDevHQ! Day 29: Damage VFX using animated sprites in Unity.
As our game is now, when our ship runs into enemies, we take damage, but there’s no visual indication to the player that anything’s happened. Better videogames show that we take damage by using particles, shaking the camera, having the player sprite react with a knockback, etc. Our game will show smoke pumping out of either side of our engines.
Just like the explosion yesterday, we’ve been giving the vfx sprites and we’ll implement them in very much the same way.
if you have a set of sprites to animate, there’s actually a quick and easy method of creating the animated sprite and that’s to drag all of the frames out into the scene and then save the _anim file when the popup box asks. So let’s do that for our trail of smoke which is located in the player_hurt folder.
The graphic is too big, so we’ll resize the scale to .5 on the x and y. Also remember to set the sorting layer to foreground and put it on layer 1 so it gets put in front of the player.
Next we’ll rename and prefab it, while we’re only going to use 2 of them for our ship, maybe there’s a chance in the future we’ll need to use them again for a different ship or object.
Next, parent the first one into the Player and grab another from the prefabs and drag it into the player as well. Place them on top of the left and right ‘thrusters’, and for simplicity's sake, we’ll name them Left_Thruster and Right_Thruster.
I made a mistake and I didn’t override the scaling of the image. I left it in as a reminder to always remember to hit the override button when you make changes to a prefab. Same with setting the sorting layer. Remember to place it in the foreground and set it a level or two higher so it gets placed in front of other sprites.
Now turn them off, we’ll be turning them on via the Players code. Just like the asteroid, we’ll create some spaces in the players ship for the smoke fx.
Wait a minute! There’s not a [SerializeField] in front of those holders!
Don’t panic! Remember we named the objects Left_Thruster and Right_Thruster? We’re going to tell unity to literally find those objects and assign them to the holders! We’ll do this in the Start() function since all of our initialization stuff happens there.
Just to be safe, let’s also turn them off in code. You never know if you’ll leave one accidentally on while making changes in the scene editor.
Now what we want to do is turn them on in a sequence or randomly when the player takes damage. I want to keep things simple so whenever a player takes damage, it will see how many lives there are, if it takes one hit, and 2 lives are left, the left thruster will turn on. if it takes another hit and 1 life is left, then t he right thruster is activated. We can do this through a set of if/then statements but remember the switch/case made things a lot cleaner? This is a perfect time to use it so let’s do that and place it in a function I call DamageShip()
DamageShip is called right after lives- -; in the public Damage() function. It checks the variable _playerLives to see how many lives are left, and turns on the left thruster if the player’s been hit once, and the right thruster if the player’s been hit twice. I put in a default with nothing in it since I understand it’s best practice.
So let’s see it in action!
Next, we’ll check out post processing and how it makes your game look even more awesome!