Project 1: Space Shooter
Using animated sprites to make damage VFX in Unity
And randomizing its appearance
Thus far the only way that the player can see that they have been hit has been the lives’ display. In this article I show how to visualize it also on the player as well.
The first thing is to add the damage sprite as a child object for the player object. After that is making it correctly sized and in the location where I want it to be.
Then comes the animation, which is done in the same way as in the previous cases. And then the last thing is to duplicate the damage and position it in where I want it to show. Since I don’t want them to show up once the game starts I hide them by switching them off.
In the Player script I make a new GameObject type array for saving both of these objects. While I’m at it I will also add an array for the thrusters that I have.
For both of these it is important to maintain the order, for me it is left at index 0 and right at index 1. Once these are added to the code, the damaged engine objects and the thrusters have to be saved in Unity.
So that my Damage method doesn’t clutter up too much I make a new method called EngineDamage that handles the actual visualization of the damage. The easiest way is to use the switch statement and for the argument player’s lives.
When the player’s lives drop to two I randomize which engine takes the damage. This is done with the Random.Range method, when used with an integer type variable the method is half-open interval with the maximum number excluded. Of course the randomness might not show often since there are only two options, but it’s the small things that matter. Right…?
Once the decision is done the engine damage object is activated with the SetActive method and the corresponding thruster is deactivated with the same method. When the player’s lives drop to one, depending on which side has been damaged first the other one is activated and deactivated to show even more damage has been taken.