So why should the enemies be the only ones to have something animated when bad things happen? My player should have as well! This time though the animation is done a bit differently. I will add it basically the same way as I added the shields powerup.
So to do that, first I need to find the correct sprites from the Filebase. Take the first one, and attach it as a child to my Player GameObject. Then find a suitable place for it, I pick the right wing and rename the object as Right_Engine. After that the animation part is done exactly the same way as before. I name the animation Engine_Failure_anim, so that I can multipurpose it later if I want to.
Once that is done, I make a copy of the Right_Engine GameObject, move that to the left wing, and rename it as Left_Engine. Then I have to deactivate both of them, so that it wouldn’t seem like both of the engines have failed before the game has even begun.
Now, how to make them visible once the player has been hit? First I have to tell my Player script of these two objects. The easiest way to do this is to serialize a GameObject array for the two. This is something that I’ve done before. I choose the Right_Engine as the first element, with index 0, and the Left_Engine for the second one, that is index 1. The order isn’t important, but this way I remember which is which a bit better.
Then I make a new method called EngineFailure, there I use the switch statement with the _lives as a parameter to determine which one should be activated. To make the damage seem more realistic, when the player loses their first life, the engine which fails is picked at random. This does make the case for player having their second life taken away a bit more involved.
Though not that involved, thanks to Unity and its method activeSelf. This method returns a bool type variable which tells if the GameObject is active or not. So all I have to do is to find out if the engine at index 0 has been activated. If it is activated, then I know that the Right_Engine has been damaged and I need to active the Left_Engine, which is the one at index 1. And vice versa in the else statement.
All I have to do now, is to call my method right after the player loses a life to have the engines fail and show the animation for it.