Day 64 — Implementing 3D Animations in Unity

Connor Fullarton
5 min readMay 18, 2021

Hey and welcome!

Got a fun one here where we’re going to be switching out the current capsule we have for the player with our Darren game object and giving him some animations too!

Let’s get started by unchecking the mesh renderer component on the Player object and then drag and drop Darren_3D from Game > Prefabs into the player so that he becomes a child of Player. Then adjust the capsule collider and Nav Mesh Agent using the options for height and radius in the inspector so that they are both big enough to cover Darren.

Run the game to check that the collisions are fine and you should have yourself a Darren object sliding about the scene. It’s not very lifelike having him slide about so let’s get started with adding some animations to him, start things off by creating an Animator Controller in Game > Animations > Character > Darren via the right-click menu. Name this Darren and double click it so that you can bring up the animator window.

Now drag and drop your idle animation into the animator window followed up by the walk and throw animations that are also found in the Animations folder so that you have something like this:

If you didn’t put Idle in first then it won’t be the default state which is depicted as orange. If that’s the case then you can just right click the Idle animation box here and set it as the default state. Add an animator component to your Darren_3D object and then slot in the Darren controller in the controller option.

Done correctly you should have the above animation on Darren that will play when he’s not moving though we’ve not added that functionality yet so he’ll keep doing that animation regardless for now.

Setting up the Walk Animation

Let’s look at getting our walk animation working when Darren moves but first we’ll need to to a little bit of set up. Double click your Walk animation in the Animations folder so that you open it in the animation window and go ahead and delete the two soft walk animation events in there.

If we had tried testing out our Walk animation it would have come up with an error because of the events which would cut the animation short. In the animator window create two transitions with one going from Idle to walk and the other going in the reverse by right clicking both animations and selecting the Make Transition option.

Click on these transition lines and in the inspector and you’ll see an option called Has Exit Time, make sure you uncheck that as it will wait for the animation to end before movingto the next state if you don’t. Now create a new bool parameter called Walk in the animator and then add a condition for it in each transition so that we move to the Walk state when the Walk bool is true and then back to the Idle state when it’s false.

Now update your Player script with this code:

public Camera camera;
private NavMeshAgent _agent;
private Animator _anim;
private Vector3 _target;
void Start ()
{
_agent = GetComponent<NavMeshAgent>();
_anim = GetComponentInChildren<Animator>();
}
void Update()
{
if (Input.GetMouseButtonDown(0) == true)
{
RaycastHit hit;
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Debug.Log(hit.point);
_agent.SetDestination(hit.point);
_anim.SetBool("Walk", true);
_target = hit.point;
}
}
float distance = Vector3.Distance(transform.position, _target);
if (distance < 1)
{
_anim.SetBool("Walk", false);
}
}

The important parts to take note of here are the two new global variables, a _anim and _target. Since our Darren_3D object is a child of Player we can use GetComponentInChildren in order to get to the Animator component and store it in _anim which let’s us use the SetBool method in order to change our Walk condition in the controller to true or false. You’ll see here that we set it to true when we have a point of impact.

You should recognise Vector3.Distance from the 2D course but as a reminder this returns the distance between the a and b values you pass into it and returns it as a float. So the short of what’s happening here is that we’re setting the Walk condition to false when the player gets close to the point of impact by passing in the point value for hit and the position value for Player.

With all that done you should have something looking like the above! If the movement for Darren is a bit too slow/slidy or even too fast for your liking you can adjust some of the settings in the Nav Mesh Agent component for the Player in order to get something you like, it’s best that you play about with this yourself.

If at any point you come across an issue where the walk animation doesn’t play even though you know that the trigger for setting the Walk bool as true or false should be working then you may need to just restart Unity.

--

--

Connor Fullarton

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.