Add a running animation to a character in Unity
Objective: Add a running animation to the player animation controller and move between idle and running
Now that the idle animation was added in the previous article, it is time to add in the running animation and set up the animator inside the player class.
You should have downloaded and imported your running animation and have it at the ready.
Drag in the running animation into the animator, once it is there add a transition between idle and running. Right-click on idle > make transition and click on the running animation box. This will create a white line between the two.
I am going to changing from idle to running by using a float parameter inside of the animator. On the left of the animator window click the parameter tag, then the plus icon and select float.
Remember the name of the parameter that you set as you will need it for the code.
Click on the white line going from idle to running. In the inspector there are a few things that need to be changed.
- Untick exit time
- Change the transition duration to 0
- Add in the parameter that you set up, for me that is Speed.
- Select greater than and the float to 0.1
This means that when the float parameter called Speed is greater than 0.1 the running animation will play.
Now add another transition but from running to idle and follow the same steps as above with 1 change. Make it so that the speed is less than 0.1.
Now open your script that has your player movement code.
First I set up the animator by using a serialized field and adding the animator in the inspector.
Above you can see the variable that I am using for my player movement horizontalInput. I am then setting the animator float called Speed to match the movement of the player.
If the player moves left then the input float would be between 0 and -1 and this will not work with how the animator has been set up. By using Abs the float will now be positive no matter the player direction.
And that is all I am doing to change from idle to running and back again. Check out the next articles for the jump animation and then the double jump.