In this lecture, I want to address an issue that people have asked me a lot about in the Q&A section
and that is synchronizing the agents speed with that of the navmesh agent. As you can see here,
I've got a very simple waypoint system set up on a navmesh and Ethan demonstrating how his running
animation is going way too fast for what he's actually doing, so it needs to be slowed down but somehow
it needs to be synchronized with the speed of the agent.
Now when I talk about the speed of the agent (let me just stop playing this),
if we go back to our actual agent and have a look over in the Inspector, there's a 'Speed' that you can set.
This speed is three point five.
Now this is ALWAYS 3.5.
It's the speed that you're allowing your agent to 'steer at'/'move between points at'.
But it's not necessarily how fast he's going because he's going to slow down into corners and then accelerate
out of them.
So the value that you actually want to have driving your animations is the 'velocity' of the character
and I'm going to show you how to assign that to the animations.
Ok,
so you can grab hold of this particular project, it's attached as a resource.
Select 'Environment' and make it into
a navmesh, so click on your Navigation tab or find it under
Window > AI >Navigation and just make sure you click on [Bake] so that you do see this blue area when you try and
run,
otherwise you'll get an error.
When you press Play, you'll see exactly what you're seeing here.
Now I do have Ethan's animations turned up really fast on purpose, just to demonstrate how you can get
animations that don't actually fit with the speed of this.
Now the character himself is driven by the navmesh agent, so the speed and everything. The animation,
if we have a look in the Inspector at the animation, I've got 'Apply Root Motion' turned off because we
don't want the animation system trying to move the agent while the agent is actually being driven by
its own code,
so that does have to be off.
Ok.
So how do we sync up these two values?
Well first of all we need to go into the Animator with Ethan selected and have a look in the parameters
list.
Currently it's empty.
We're going to set a parameter in here,
so we're going to add a Float and call it velocity or 'vel'.
That will do.
We can leave it set to 0, we're going to change this with the code.
Now once you've got that, select the animation (there's only a HumanoidRun on there at the moment)
then over in the Inspector you'll see that the speed currently for HumanoidRun is set to 3.
We're going to multiply that speed by a parameter, so select that
and then if you drop down the list next to it, you'll get 'vel'.
So any parameters that you have in here, that are applicable for multiplying by speed, you can put in
this dropdown list and control with your script.
All right.
We've got that, let's now open up the script that's on Ethan, which is this FollowPath
and first we need the Animator.
So let's get hold of that with 'Animator anim;' and then we will pick that up in our start with
'anim = this.GetComponent' and then Animator and then finally, this is just a simple waypoint system going
through each waypoint and updating them as we reach the destination.
After we've done that, we're going to set the speed.
So this will be 'anim.SetFloat("vel"
and the value that we're going to set that to.
Well that's the velocity and you can get that off the agent with 'agent.velocity'.
Now that's a vector.
You want the length of that vector.
So '.magnitude' because that vector will change length based on how strong the velocity is.
Okay so now this magnitude will be fed into your velocity. Save that once you've got it. Let's go back
to Unity. Have the Animator window open so you can watch this 'vel' change with the speed of the agent.
So press Play and then look over here. You can see how it changes and it actually goes down and then up again
as he accelerates out of each of those.
Now when currently multiplying 3.5 by...what's on here...3.
So that is just way too fast as you can see there,
so let's just stop that.
Let's turn our speed multiplier down to say 0.3
and then again press Play and now you'll see (let's go back into the Scene, so that we can see it better)
that he is now much better on that surface.
He's not quite touching the ground, but we can fix that.
That's a navmesh thing.
If we select Environment and go to Navigation, we can then drop down 'Advanced' and turn on the
'Height Mesh' and then bake that in.
That will allow him now when he runs to sit on the actual mesh a little bit better.
That, of course, doesn't affect the animation whatsoever.
But now you know how to pass through values from your script to your animator and then control that
so that you have a more realistic procedural animation going on.