In this article, we will add animated sprites for visual effects to the Player, like thrusters and damage smoke trails. I have made several looping animated sprites in previous articles, so I will show how to do it and not write it out in detail.
Drag the Thruster sprite to the Player to place as a child, adjust the position, and then animate it.
Drag the Fire Trail sprite to the Player to place as a child, name it “Right Engine”, adjust the position, and then animate it. Duplicate it, move it to the other side of the Player, and name it “Left Engine”.
In the Player script, create a private array of Type GameObject named _engineFires with a null value; this will reference the two engine sprites and how they are turned on/off. In Start, add a for loop that will iterate through the _engineFires array with i as the index and set each one to false. A for loop will go through every number until the local int i is greater than the value you set. In this case, it is the number of elements in _engineFires which is 2, so the for loop will start at 0, check if it greater than 2; if not, it runs the code, then increases i by one and repeats till i is greater and then stops the loop.
In the ChangeLives method, after _lives has amount added to it, create an if statement that checks if _lives are equal to 2; if so, change SetActive of the first element (0) of _engineFires to true. Then create another if statement that checks if _lives are equal to 1; if so, change SetActive of the second element (1) of _engineFires to true. These will activate the fire trails one at a time as the Player takes damage.
With that, the Player is looking good.
In the following article, we will look at the Unity Post Process package and apply it to the game.