Adding a Running Animation
Importing a Running Animation and working with animation Transitions
Now that we have our Idle animation down, it is time to work on our running/walking animation. We will follow through the same procedure in import from Mixamo library.
After picking the correct animation and importing it to the Unity projects, we are going to drag and drop the animation clip into the Animator.
Now that the running state has been created, we need to make a transition between idle and Running, and back from Running back to idle. (right click and select create Transition, then drag the white line to the desired state)
If you play the animation now, you will noticed that the character will transition from Idle state to Running state automatically once the clip finishes. Since we only want the transition to happen only when movement has started, we need to add in a parameter with set conditions.
Add an Float parameter, this will be our speed variable to Trigger the running animation. On the right side, we will set the conditions to trigger the animation transition.
For the transition from Idle > Running, set the speed to be greater than 0.1. Vice versa for the opposite, we will make the speed less than 0.1. Also make sure for both conditions to make “Has Exit Time” = false (unchecked). The Animator should be set up, now we need create a script to toggle the conditions so the animation transitions can happen.
Since the movement calculations are within the Player script we will create the logic which will toggle the animation there. To begin we will need to create an handler for the newly added animation controller. Then in the Start Method we will get the reference to the Animator within the Children component (since our Animator is set within the children object).
Locate the area which we control our movement in the script, the logic behind the toggling of the animator is quite simple. Every time we make a movement to our player, our speed value increases or decrease. Since we set the parameters of the animator to be a Float Value, there is a script for the animator component which can access that value called “SetFloat”.
When you hit play, you would noticed that the player now toggles the run animation when going to the right, but immediately going back to the left the character switches back to Idle. This is because of our set conditions where we have “less than 0” to trigger the Idle animation. To fix this, we need to add in a simple code to convert the negative value into positive.
Back in the player script we just need to add in Mathf.Abs(horizontalInputer); to convert the negative number into a whole one.
With that added, the character should toggle the running animation when going left or right, although the character isn’t facing the proper direction, we will address that later on.
If you find that your character is rotating while moving, this is caused by having the “Apply Root Animation” check box being active. Uncheck to get rid of this issue.