Platformer: Player Animation Part 2

Ryan McCoach
4 min readDec 5, 2021

Intro

In this article, I will go over how to control the animator through script to transition between idle and running animations.

Download

When downloading an animation with motion from Mixamo make sure to check off In Place. This will allow use to control the movement via the script and not the animation.

Animator Controller

After importing the running animation, we drag and drop the animation into the Animator Controller window and make sure we set Loop true.

In the Animator Controller window, we need to add transitions from the Idle animation to the Run animation and from the Run animation to the Idle animation. This will allow us to control when we want to transition between the two animations through script.

For both the Idle and Run animation, we want to change a few of the transition settings. The first being setting Has Exit Time false, this will allow us to transition from one animation to the other without waiting for the animation to finish playing. Second, we will set the Transition Duration to 0, so again, the transition is instantaneous and doesn’t have a blending of the two animations.

In order to know when to transition from one animation to another we need to add a Parameter. We will add a float parameter and name it Speed, since it will be controlled by the horizontal input. Once we have the parameter, we need to create a condition, that once met, will trigger the transition. The condition to transition from the Idle to Run will be the Speed be greater thant 0.1.

Now, we will need to set a condition to transition back from Run to Idle. This will be Speed Less than 0.1

The Code

We need to access the Animator component, so we create a handle and assign it in the On Start using Get Component In Children, since the Animator is on the Child of our Player game object.

Where we are calculation the left and right movement is where to want to communicate with the Animator when to transition between the Run and Idle animation. Using the SetFloat method, we can pass in the name of the parameter when want to set and the value. The value is the horizontalInput.

When the right arrow key is pressed, the horizontal input will increment the to 1, triggering the animation but when the left arrow key is pressed, the horizontal input will decrement to -1. Since the Run animation condition is checking for the Speed parameter when it is greater than 0.1, it is only transitioning when moving right and not left.

To fix this, we can use the absolute value of the horizontal input, so when moving left is returning a negative value, it will convert it to positive value which will meet the Run animation condition.

We now have the run animation working when we both move left and right. The one thing is the transition is not occurring right way and this is due to the horizontal incrementing to 1 and not directly going from 0 to 1 when the left/right arrow keys are pressed.

Using Input.GetAxisRaw will do just that, go from 0 to 1 when the left/right arrow keys are pressed.

The last thing we need to check for is setting Apply Root Motion false. Root motion allows the animation to control movement and was having our player rotation when we moved.

Conclusion

We will explore how to flip the animation when we move left and add a jump animation when we jump. Until then…Cheers!

--

--