Using the Unity Animation System

Objective: Set up our Players animations.

Now that we have the Point & Click and NavMesh set up, we can move on to using our actual Player model instead of the capsule we put in place.

I’ve swapped out the capsule, added the model and set the collider and navmesh radius, height and positions.

To get started we need an Animator Controller. This controller allows the character to switch between animation states. Next add an Animator component to your model/Player and drag the previously created controller into the Animator. You can open the controller by double-clicking on it.

In the image above, you can see the default state of the new controller. We can drag in our animations into the controller. For this project the animations were given as assets but there is a great resource called Mixamo available if you have none of just want to get started.

Once the first animation is brought in you will notice the connection between Entry and Idle(Our Animation). This is now the default animation state.

Next we drag the rest of our animations into the Animator.

In order to swap or move between animations, we need to set up the transition. To do this right-click on the Idle and chose make transition, then select the Walk to great the transition link as shown in the image below.

We will to repeat the same action but in reverse(Walk to Idle) to set the transition back when the Player stops moving. We want transition time to be immediate instead of allowing the animation to play through. For this select the transition arrow and in the inspector, turn off Has Exit Time. We do this on both transitions between Idle->Walk and Walk->Idle.

Next to trigger the animation transition, we need to set parameters. We will use a bool for this transition. In the Animator window, select parameters and click on the + icon, select bool and name the parameter.

Move back to the transition arrow between Idle and Walk and in the Inspector you see Conditions. Click to add and choose the Walk and set to true. Open the opposite transition between Walk and Idle and use the same condition but this time, set it to false. We now have the behaviour of the animations set.

We will use our Player script to activate and de-activate the animation transitions. We can’t use GetComponent directly as the Animator is in a child object of the Player. Instead we can use the GetComponentInChildren call.

We create a private Animator _anim and assign it in the Start method. We can now call SetBool using the Walk paramater and set it to True in the Update after we’ve called SetDestination. This will transition our animation from Idle to Walk. All thats left is checking when the Player has stopped walking and set the animation back to Idle.

For this we will check if the Player is within distance of the destination that was set. Because our earlier code creates the destination information within the Raycast scope we do not have Global access to this information. To solve this we can create a Vector3 globally and assign its value to the RaycastHit values. We can then check the distance between the Players transform.position and the _target. If the distance is less than the given range (1.0f in this case), we can set the Walk animation to false, transitioning back to Idle.

In the next post, we will look at modular waypoints in Unity.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store