Using the Unity Animation System (Game Dev Day 59)
I added animations to the new model for the Player Character using the Animator window in Unity.
First off, I added an Animator component to the 3D model attached to my player gameobject.
Next, I opened up the animator window through:
Window>Animation>Animator
I have 3 animations for the player character; Idle, Walk, and Throw.
The golden arrow from “Entry” to “Idle” means that “Idle” is the default animation to play. It will ALWAYS play unless it is told not to.
Next, I added two transitions between “Idle” and “Walk” indicated by the white lines. Turn off “Has Exit Time” or the entire current animation has to finish playing before the next one plays (leads to a delay in the animation).
We will come back to “Throw” later, as that needs some extra functionality before we can animate it.
Open the parameters tab inside the Animator Window.
I created a new bool parameter titled “Walk”. This bool will be used to determine if the idle animation should be playing (default) or the walk animation.
Now in the Player.cs script I added the Animator component in Start.
Next, I added line 30 to the existing movement input code.
So when you click to move, the “Walk” bool activates, changing the animation state to the walking animation!
Getting back to the “Idle” animation requires a little more work to do smoothly.
I created a new float variable and used Vector3.Distance to calculate the distance between the starting point of the player and the coordinates of the location they are traveling to. Once that value reaches 1 or lower, the “Walk” bool turns off, returning the player to the idle animation!