Today, we will learn how to make a transition between two animation: idle and run.
Go to Mixamo and find run animation that you like. Download it and
Drag the file you downloaded to your Unity project.
Click on the file, go to Rig and change Animation Type to Humanoid.
Click on the run Animation and press control + D to duplicate the animation.
Click on the duplicated animation and make sure that Loop time is checked.
Go to Animation Controller view and drop the Run animation.
Click on one of the animation with right mouse button. Click on the Make Transition.
Make transition from idle to run and from run to idle.
We will create a game logic so that we will transition from idle to run only when we are moving. To do that we need to add a parameter to the Animator Controller. Create a float parameter called Speed.
Click on the transition from idle to run. Add a condition to it and uncheck the Has Exit Time option.
In the transition between run and idle change the speed condition to be less than 0.1 and uncheck the Has Exit Time option.
Go to Player script and add a new variable.
Access this variable from child GameObject that has Animator Controller component.
Use _anim.SetFloat() method to change the Speed parameter in Animator Controller. Since horizontal variable is going from -1 to 1 we will use Mathf.Abs() to get absolute value. Also, change Input.GetAxis() to Input.GetAxisRaw to remove the delay that is happening when we move our character.
If you are using Ninja Idle animation you will also need to change the rotation of the animation. Right now we are moving from left to right but our idle animation is not rotated correctly. Go to idle animation and in Root Transform Rotation and change Offset to 90.
We will also check the Mirror option on the animation.
The last thing is unchecking Root motion from Animator Controller component. This way our character will only move and rotate based on the Character Controller, not animation.