Damage VFX Using Animated Sprites In Unity
Hi! So, in todays article I am going to talk about how to trigger damage VFX on your player. Now, you can make it go to each individual side when a certain amount of damage is met, however I randomized mine so the damage can trigger on either side randomly when the player gets damaged. So, here is an article on how I went about doing that.
Mission: Create damage effects when player gets damaged.
Side Mission: Randomize which side the damage is on.
So, to create the damage effects I first created an animated sprite and sized and set it where I wanted the damage to occur, which in my case was the engines. After doing this and placing it on one engine and making it look good I simply duplicated it and moved the duplicate to the other engine. After that, I had to disable it and then code it to be active when I wanted it to be. So, to do this, I turned of the sprite renderer of the damage sprites.
Then I had to code it to turn on when I wanted it to. So to do that I needed a way to reference them. So I made an array and serialized it so that I could drag and drop the damage engines into the array spot in the inspector.
Now that I had a way to reference them, I went to where I would reference them in my script which was when my player took damage. So I went to my damage method that I had in place for my player and set the references.
I also made another script to check if the engine was damaged or not and put it on my damage sprite objects. It was a simple script with a bool that checked if the engine was active and functioning or undamaged, with a method that switched it to false if it was damaged.
This way I had a way to check if the engine was damaged or not. Then I went back to my player script. So, to randomize the engine damage, I used the random.range function to select one of the engines at random when the player lost a life or a hit point. I did a null reference check to avoid errors and got the sprite renderer component from the random engine, then enabled it. Doing this I was able to randomize which side the engine damage would occur on. I also used the method from my engine damage script to set the bool to the engine being damaged or inactive.
Then, when more damage came, I just had to make sure both sprite renderers were enabled and both bools were changed by communicating with my other script.
After that, I just made sure everything was set in my inspector and I had a functional random engine damage visual effect on taking damage. Thank you for reading =).