Animate the 3D Model by Using Animator- Game Dev Series 68
Objective: create different state of animation in Animator.
Animation is what can bring our 3D models come to live. In Unity, we can even switch between different animation through states. Which is the basic animation function in Unity.
In this article, we will create animation state to make our character animation switch between idle and walk.
Intro of Animator
To create an Animator, we can simply right click on Project and select Animator. Double click and it will open the Animator window.
You would see some default states have set.
Now we can drag in our animation from Project. In here, I would drag in Idle & Walk animation.
If your animator has not set the Idle as default state, you can right click Idle and select as default.
Now we can set the state transition to our animations.
Set the transition
Right click on the Idle and select Make Transition, then click on Walk. It will create an arrow from Idle to Walk.
Since our state should return as Idle from Walk, let’s do it again from Walk to Idle.
It should looks like this.
With transition, we will need to add a parameters to it. Click the tab Parameters on the top left of Animator tab and click Add button.
In here, we will use bool as parameter.
Let’s assign this bool to both of the transitions.
Now our animation is ready, we can assign it to our Player.
Assign the animator
Select the 3D model of our Player and add an Animator component. If the model is a child of Player, you should add the component in the child.
Then drag the animator that we create into the Animator component.
Next, we will make our Player to access the Animator in script.
Access the Animator from code
Open Player script, we need to add a variable to access the component from child.
In Update(), we will make it set the bool to true when the Player has a destination from our mouse clicking. We can use NavMeshAgent.remainingDistance in here.
Save script, now our Player should have Idle & Walk animation when playing!