In this lecture, we're going to look at how to set up multiple navmeshes and assign different sized characters
to different ones.
This will allow you to specify where different types of characters can and can't move. To follow along,
download the AgentSizesStarter file that comes with this lecture and open up the BridgeTest scene.
In that scene, you'll find 2 characters. We've got a guard and we've got a girl
and if you just rotate around, you'll see that we've also got a castle wall with different-sized holes
in it.
So what we'll do is get the girl (which is the smaller model) to go through this narrow opening and
make sure that the guard cannot get through there because he's too big.
The same for that bridge...she'll be small enough to go across the bridge but he won't.
The first thing that you're going to have to do is set up these characters for use in Unity.
These are characters I've brought in from Mixamo. I've fixed their materials but I haven't done anything else so
we're going to go through the whole set-up for their animations.
The first thing you'll need to do is select each model and set it to Humanoid.
So I'm selecting Andromeda (which is the girl) and over in the Inspector,
I want to go to 'Animation Type > Humanoid' and then press [Apply].
Then you want to do the same thing for the castle guard...select it in your project and then create a Humanoid
out of it and hit [Apply].
The next thing to do is set them up with their strafe set.
Now, this project already has the animations and the scripts for that strafe set included.
Open up the Animations folder and this is where you'll find that 'Locomotion' animation controller that was created
by Unity. Select the castle_guard and in the Inspector you'll find the Controller slot is currently empty.
That's where you want to drag-and-drop this LocomotionController, into there.
Do the same thing for Andromeda, as well.
So just select her in the Hierarchy and drag-and-drop that LocomotionController on to her. Next, you want
to add the ClickToMove script.
So if we go into the Scripts folder, these are the same ones that come with the characters for the strafe set.
If you select your character (so Andromeda in this case) we'll drag-and-drop ClickToMove on to her and
we will also drop LocomotionSimpleAgent script.
I won't worry about the LookAt script.
You can set up LookAt if you want. Do the same thing for the castle_guard...we want to drag ClickToMove
and the LocomotionSimpleAgent script onto there.
Now that will automatically add the Nav Mesh Agent component on to each one because the LocomotionSimpleAgent and
the ClickToMove require that. Now we also need to make a navmesh out of our map. So select the Map,
then we want to set it to Navigation Static and all the children that are underneath it because that
would include the ground and all that.
I just noticed that the bridge itself isn't part of the map.
I'm going to add that down into there and we can make that Navigation Static as well.
So select the Map, go to the Navigation tab (open up that window if you don't have it) and we'll go to
Bake and just [Bake] as it is by default.
So you can see there that you get a navmesh that will cover everything and then there's a really
narrow one that goes across that bridge.
If you check all of the links, you'll find that there's enough of a step and touching where
these meshes join together.
So with that set up as the default,
if we press Play and then click over here, we'll find that our agents go for a little walk to get to
the other side.
Now currently we haven't set different sizes for the different agents, they're just all the same default
agent and that's all based on the Baked Agent Size that is in the Navigation.
Now while I was just running it,
you might have noticed a bit of 'jitteriness' in the characters movement.
Let's take a closer look at that.
First of all, let's turn off the castle_guard just so he's not there and we'll do this on Andromeda.
So select Andromeda so that you can see her navmesh agent cylinder, as well as her and then we'll
press Play
and what's happening in that script is that it's trying to line up the animations with the cylinder
and when it goes jittery, you'll see that she actually gets ahead of that cylinder with her animations...
just there, you can see that happening.
The reason why is because the animation is a faster running animation than the speed you're allowing that
agent to move.
So to fix that you've got to select the character in the Inspector and for its Nav Mesh Agent, increase
its Speed and that will go a long way to fixing that
jitteriness. So let's make her speed 8.
We'll go back for the castle_guard and turn him back on and we'll change his speed also to 8 and that
will make the movement for them a lot smoother.
Now he can currently run across the bridge because we don't have any restrictions on the size.
What we'll do now is we'll create a different type of agent and assign that to the castle_guard, so let's
stop playing this.
Then in the Navigation window under Agents, we'll create another agent type.
We've got a Humanoid in there
but click on '+' to create a new agent and we'll call this one Guard
and then modify the size.
So let's make him 1 round and let's set his height to
3. The Step Height should still be OK because it's alright for the basic character (the Humanoid) which
we'll leave on the Andromeda model.
Now to assign this 'Guard' to the castle_guard,
click on the castle_guard and go to the Inspector.
Have a look at your Nav Nesh Agent component
where you've got Agent Type and it's in there that you want to set that to 'Guard'.
So that gives you a different type.
Now at this point, if we press Play, we're going to get a common error that people have been getting so
press Play and click over here and we get this error that says 'SetDestination can only be called on an active
agent that has been placed on a NavMesh'.
Which is true, in a sense that the navmesh in the Navigation
(the one that you've baked) is only working on that default Humanoid, yet you've got another agent called
'Guard' that YOU think is sitting on that same navmesh, but UNITY is saying "No, I don't know about it".
So what we need to do is use some beta code that has been developed and it's not part of Unity yet, but
you can get it and put it into Unity.
So instead of using THIS navmesh generator, we're going to use a different one.
What I want you to do is bring in the package that's called 'AINavMeshComponents' that came with this lecture
and I'm going to just drag-and-drop that into the Assets folder.
Now in there,
there's a number of different scripts that we can apply. The one we're interested in is the one that's
going to allow us to have multiple navmeshes on our map. So select the parent of the terrain (which in
this case is our map) and go to the Inspector.
We want to find the Scripts folder that came with our newly-imported NavMeshComponents which is
just here and you're looking for the script called NavMeshSurface and that's the one we want to
drag-and-drop onto our Map. We can also select [Add Component] and find it in this list as well.
We actually need 2 of them, so we'll put another one on there. We want 1 surface for the Humanoid and
1 surface for the Guard, so this top one we can leave as Humanoid
and then the next one down, we can set that as Guard.
Now if you click [Bake] for both of these, you'll get different navmeshes appear and you can see them in slightly
different colors.
So you've got one here and you've got one there and you've also got the old one hanging around from
the default. If we just go back into the Navigation window and [Clear] that one, then it won't be a problem
for us because we don't need it anymore.
So back in the Inspector
and we can see those two.
Now what's happened here is that the water has been picked up as part of our navmesh.
We don't want the water to be included.
So over in our Nav Mesh Surface settings, we can go to 'Include Layers'
and currently it's got everything, but we can go down and select 'Water' to untick it and ignore it and
we can do that on both different types of navmeshes and then [Bake] both of them back in.
Then you'll see the water is now excluded.
So the Guard (castle_guard) will travel on one navmesh and the Humanoid (Andromeda) will travel on the other and you can
see over the bridge that only Andromeda is able to travel because the Guards agent is too wide.
So let's try this out.
Let's press Play...and we'll find that if we click over here, we've got Andromeda going across the bridge
and the Guard going through the water.
The other thing that we've got as well (if you just zoom in to the castle itself) is that Andromeda
can go underneath, through this little narrow bridge part
and both of them can go through the wider one.
So if I go back to the game and just click over here, you'll find that she'll go through the middle because
she can fit, whereas he has to go around the outside but if we move them both over here and then ask
them to come here,
they'll both go through that bridge because they can fit.
You can add as many different navmesh surfaces onto your terrain as you've got different types
and this is the only way that you CAN have different types of agents. Even though the Navigation window
allows you to have multiple agent types, unless you use those surfaces then the default navigation that
you bake in, using this part here, is not going to find your other agent types and you'll get (as we saw)
errors.
So that's using multiple meshes and multiple agent sizes.