My 90 day journey becoming a Unity game developer: Day-32

Objective: Player damage visualization with animations.

Added engine failure animations to the player when damaged.

Let’s begin with dragging the first Player Hurt sprite onto the Player. Scale down the size of the sprite to a more appropriate size relative to the ship. Drag the sprite over to the right side of the ship. In the Inspector, come down to the Sprite Renderer and change the Sorting Layer to Foreground. Also, change the Order in Layer to 1 which will place the sprite in front of the ship’s right wing. Change the name of the sprite to Right_Engine. Duplicate the game object using CTRL-D for Windows or CMD-D for MAC. Name it Left_Engine, then go over in the Transform position and add a negative sign in front of the X-value to shift the new sprite over to the left side of the ship.

Damage sprites added to the left and right sides of the ship.

Next, turn off the game objects to the Left_Engine and Right_Engine as they will be activated using code.

Inside the Player script, create 2 variables that will give us access to Left_Engine and Right_Engine. Down in the Damage() method under the lives being subtracted when the player is damaged, set up if/else conditions that will check if the player has 2 lives remaining to turn on the right engine. Else, if lives equals 1 remaining to turn on the left engine.

Setting the engines to be turned on when conditions are met.
Engines activates when player is damaged.

To add animations to the damaged engines click on the Right_Engine game object. In the Animation window, click on Create and name the animation Engine_Failure or something to your liking. Drag all the Player Hurt sprites into the dopesheet. If you want to see the animation play, make sure the Right_Engine game object is turned on and click on Play in the Animation window.

Testing the engine failure animation.

Turn off the Right_Engine game object because the code will control when to turn it on. For the Left_Engine, you will need to add an Animator component. Go to the Animations folder and we look for the animator controller that was used for the Right_Engine. The controller name was different from my animation clip’s name. It was called Right_Engine, while my animation clip is called Engine_Failure. I ran into a Unity 2020.3.7f1 bug problem when I tried to change the name of the controller to the same name as the animation clip. When I changed the name, my animation was erased from the controller and I received a message complaining about the meta file.

Problem with changing the animator controller’s name that deleted the animation.

After a couple of hours of trying to figure out what the problem was, I shut Unity down and restarted it to find out by accident that the new name change was excepted and the animation clip was restored. I looked in the Animations folder under the Project itself to see that the files were stored correctly.

Inside the Left_Engine game object, drag the Engine_Failure controller into the controller slot under the Animator. Make sure the Left_Engine is turned off as well.

Both damage engines animations are working.

Next time we will install the Post Processing package.

Learning to become a Unity game developer.