Using the Unity Animation System

Christian Carter
3 min readNov 2, 2021

--

This tutorial will cover the basics of using the Unity animation system. The first step is set your player back to it’s default settings and turn off the mesh render so the capsule will not be displayed.

Next, add your prefab character as a child of your Player. Adjust the size and position as needed.

The next step is to adjust the capsule collider so the it fits nicely around your character. You can do this by adjusting the Radius, Height, and Center of the collider. You also want to adjust the Nav Mesh Radius and Height so they fit better around your character. One thing to make sure you have set up right is the bottom of the Nav Mesh should match with the bottom/feet of your character otherwise your character may float above the ground.

Now that the 3D model is setup the next step is to setup the animator controller. To do this in your animation folder with your character animations create a new animator controller. Attach it to your Character.

Next, drag your animations into the Animator window. Make sure your idle animation is first so that it is connected from the Entry.

You now need to add the transitions from Idle to Walk and from Walk to Idle.

Next, add a bool parameter called walk. Then in the condition for the transition from idle to walk set the walk parameter to true. Then in the condition from walk to idle set the walk parameter to false. Also make sure you uncheck the exit time setting. This will set it so that when walk is true the walk animation will start playing and when walk is false it will return to the idle animation.

Now you need to create add the code to trigger the walk animation when you start moving. Go to your player script. Add an Animator variable. Assign it your animator component. If you animator is in a child of your player remember that and use GetComponentInChildren.

private Animator m_animator;void Start()
{
agent = GetComponent<NavMeshAgent>();
m_animator = GetComponentInChildren<Animator>();
if (m_animator == null)
{
Debug.Log("Player Animator is null.");
}
}

Then in your Move function use SetBool after you verify the Raycast hit to set the Walk parameter to true. This will start the walk animation when you click.

if (Physics.Raycast(ray, out hitInfo))
{
agent.SetDestination(hitInfo.point);
m_animator.SetBool("Walk", true);
}

Next, you need to add the code to set Walk to false so the character stops walking when they reach their destination. In your Move function after you handle the clicking and moving add another if statement and check if the distance between the characters position and the destination are less than 1. If yes then set the Walk parameter to false.

float distance = Vector3.Distance(transform.position,                 
agent.destination);
if (distance <= 1f)
{
_animator.SetBool("Walk", false);
}

Now your character animations will trigger as they should.

-Chris

--

--

No responses yet