Player Animations: Idle, Run & Jump
Objective: Add idle, run and jump animations that coordinate with the input.
To begin with download animations from Adobe Mixamo for Sad Idle, Running and Running Jump. Saving them as FBX for Unity files. If the animation provides the option select in place. For games the animation should not be controlling movement. Drag the downloaded FBX files into the Unity FBX folder on your Man_01 asset in the project folder.
For all three animations, select the animation and on the Rig tab select humanoid for the animation type and apply the changes.
Look inside the xbot@Running Jump asset and duplicate (Ctrl + D) the Running Jump animation. Move the copy to the animation folder for Man 01, as shown below, reverting to the original name. The copied animation can be edited. Open the new copy of Running Jump animation and check loop and bake into pose for both the Root Transform Rotation and Root Transform Position (Y), but not (XY). This prevents the animation from altering the Players rotation or Y position, as we want the Player script to exclusively control player movement. Follow the same process for both Idle and Running animations.
Now look at the Model, i.e. Man 01, asset that was chosen to understand its structure. The Model has the components shown below. The Level of Detail (LOD) Group controls the amount of detail shown based on the distance to the camera.
This explains the presence of three models for the Man_01 corresponding to LOD0, LOD1 and LOD2.
LOD0, the highest resolution model, contains an animator component. LOD1 and LOD2 are also similar, except for the use 1_Man_01_LOD1 or LOD2 for the avatar. Make sure that apply root motion is unchecked. Create an Animation folder in project Assets folder then create an animation controller, named Player_Animator_Controller. Assign this controller to all three models.
Open the Player_Animator_Controller and drag Sad Idle, Running and Running Jump animations into the Animator window. Set Sad Idle as layer default state and create transitions to and from Running. For the Running Jump, transitions to and from running are required as well as a transition to Sad Idle, as shown below.
Create two new variables. Speed is a float and Jump is bool. The Speed is used to transition to running when > 0.1. Turn off has exit time and fixed duration. Adjust the Transition Duration to provide smooth transitions between states. The Running -> Sad Idle transition is similar except that it occurs when Speed < 0.1.
The transition to Running Jump occurs when Jump is true; _jumping is discussed later in the Player script. The transition back to Running is shown below, and occurs when both Speed > 0.1 and Jump is false.
The Player script will need two new variables to coordinate animations. The _anim variable stores the Animator to allow communications with animator parameters. The bool _jumping is used to control jumping animator parameter, Jump.
The void Update() method accepts input from the player and coordinates the input with the animations, see below. The _anim.SetBool command is used to change the state of the Jump Boolean when the player either jumps (_jumping true) or lands (_jumping false). The _anim.SetFloat command is used to pass the players _zVelocity to the Speed parameter in the animator. A Quaternion.LookRotation is used to face the player in the direction of movement.
The Player now has a sad idle and can transition to and from running and jumping and back to idle based upon player input.