Player Run Animation Part 2!

Objective: Control animation parameters with Code!

We have a run animation and a parameter, we now need a way to control the parameter. For the sake of Code readability we can create a new script named “PlayerAnimation” and at the title suggest this script will control the player animations. Attach this script to the player and open the script.

Remember, we are keeping this object very clean, there will never be any component or anything like that on this object, just the animations and the animator that control the sprite.

We need to assign a handle to the animator.

Create a public method “Move” with a float parameter.

With this method we want to set the float parameter “Move” on the animator to the float parameter inserted to the move method. We need to use the absolute value of the parameter, this will turn the negative 1 from moving left to a positive 1.

On the player script we need to create a handle to the player animation script.

Inside the player’s move method, we call the player animation move method with the local float variable we are using to calculate our movement.

Now your animation transitions will be triggered and you running animation will play when you move the character.

In our next tutorial we are going to allow the player to turn around and face the left. Don’t be afraid of experimenting with your animations and I’ll see you in the next tutorial!