Objective: Add animations to the player’s character model

Idle Animation

To start you can go to https://www.mixamo.com/#/ and create a free account to find free animations that are Unity compatible.

You will start with selecting an idle animation for the character. Search idle in the search bar and select an animation you like. You will download the animation using the FBX for Unity format and select with skin.

This will show up in your downloads. Drag it into your unity project.

You can drag it into your characters FBX file

Select the Idle animation folder and change the Rig animation type from generic to humanoid.

Use the drop-down to duplicate the Idle animation and put it into a file for the character animations.

Now create a new folder under the Projects folder to fold the Players Animation Controller.

Add this animation controller to the character model in the inspector.

Open the animation controller by double-clicking it and drag the idle animation into the controller.

Right-click entry and add a transition to the idle state

Make sure you select loop time so the idle animation will loop.

This will give your character an idle animation when you start the game.

Running Animation

Now we will add a running animation to the character. Downloading the animation and adding it to Unity will be the same as before so I will start with having the animation in Unity.

With your running animation selected and added to Unity, you will drag the animation into the animation controller. You will also create transitions between the idle state and the running state.

You will create a float parameter called Speed which will be used to determine when the running animation will play.

Click on the transitions and uncheck has exit time. You will also uncheck fixed duration and set transition duration to 0. Going to running from idle you will select the Speed parameter as the condition and have it set to greater than 0.1. Going to idle from running you will select the Speed parameter as the condition and have it set to less than 0.1.

In your player script create a reference to the animator.

After you calculate your player's movement you will set the animation to play if the absolute value of the player's horizontal movement is 1. You will use the absolute value so that the animation will play when the player is going left.

This will get the running animation playing.

Turning the player and Fixing Camera

Now let’s fix the problem that the player is not turning to face the direction he is moving.

Right after you set the animation to play you will add an if statement to check for horizontal movement. If it is not zero you will get the local Euler angles as a variable. If horizontal movement is greater than zero the y Euler angle variable will stay 0 and if it is not greater than 0 you will set the y Euler angle variable to 180. After this set the actual Euler angle to the variable.

This will flip your character to face the correct direction.

Now you will need to fix the camera flipping. This is happening because we have the camera as a child of the player. So it will flip when the player does. To fix this you will unparent the camera from the player.

Create a new script and attach it to your camera.

In this script, you will create a reference to the transform of the player and a vector3 variable to hold the position of the camera.

In the start function, set the camera position to equal the position of the camera minus the position of the player.

In the update function, you will set the position of the camera to the player position plus the camera position variable to get the camera offset.

Add the player to the transform in the inspector

This will get your camera following your player correctly.

Jumping Animation

The last animation we need to add is a jumping animation. With your animation in Unity drag it into the animation controller and add the transitions.

The transitions will have the same adjustments and the transitions to and from idle will be set to speed less than 0.1 and the transitions to and from running will be set to speed greater than 0.1.

Add a new bool parameter that will be called Jump.

This will be added to all transitions to and from the jump animation and will be set to true if it is going to the jump animation and false if it is going from the jump animation.

In the player script, create a bool variable to hold the value of jump.

When the player jumps set the variable to true and play the animation. If jumping is true and the player is grounded you will set the value to false and stop the animation.

This will allow your character to play the jump animation.

Unity Game Developer and C# Programmer