We're going to continue looking at navmesh agents in this lecture by creating another agent that can
only go on certain areas.
So, first of all, grab hold of your RedAgent and just duplicate it.
If you've got a whole bunch of other RedAgents because you were playing around with them from before,
you can leave them there or you might want to get rid of them.
Now I'm just going to press W here and move these agents apart and I'll use my blue material,
a color that I made before and create the blue agent.
So let's call this BlueAgent...and this BlueAgent, because it was a copy of the RedAgent, has the same
AIControl script and the Nav Mesh Agent attached to it.
They're important.
You need to have those in there and we're not actually changing the code at all. We're changing the
navmesh specification. What I want to do is make the BlueAgent unable to go down this corridor
and that means modifying the areas within the navmesh so that the BlueAgent is unable to walk across
this polygon that is here.
So I'm going to go back into Navigation and we can see that everything is blue and it's all set to be
walkable
in this case, that's the first built-in value.
We're going to add another one, let's call it Corridor. Now Corridor
we want to assign to this area here.
So you need to click on the polygon (or the mesh) that will be the corridor, so anywhere else you want
to set to Corridor
you can do at this time as well, but just by shift-selecting all of the parts that you want.
So let's just do this one.
Now that it's selected, go over into the Object tab under the Navigation tab. You'll see that this Navigation
Static box is ticked because that's what we set it to
back in the Inspector under Static here.
Now if you select a polygon that you haven't baked before, such as this one here, you can
select it as static here or in Navigation,
you can actually tick it and this one is ticked as well.
Ticking that does exactly the same thing as setting it
Static in the Inspector.
Now that we've created that corridor as an area for the navmesh, in this Object section we can set
individual surfaces to be that type.
So in this case, I can drop this box down and I'll find 'Corridor' because I typed that into the areas and so
that polySurface4901 will become the corridor.
If we go back into Bake and then bake it, you'll see that it goes pink and let's just check out the areas
and you can see that the color for corridor is pink.
Now you might've just noticed in here I've got this Height Mesh ticked because I was testing something out
before. Let me just bake again without it.
Right.
So now we've done that, we've assigned that to the corridor.
The next thing we need to do is tell the BlueAgent that it's unable to go down corridors.
So we go back and find the BlueAgent which is here and in the Inspector at the bottom of its Nav Mesh Agent
we don't change the Agent Type.
We're still working with Humanoid but we do change the Area Mask which is right down the bottom and
in here it shows you all the different parts of the navmesh that will be navigatable by that agent
and in this case the corridor isn't.
So we just select it and that will turn corridor off.
Now the BlueAgent cannot go down the corridor.
So let's test this out.
We haven't changed any code, we've just changed the navmesh and where the agent can go. Press Play and
we've got our 2 agents here.
They'll be able to wander around
but if I select the inside area, the RedAgent will be able to get there
but the BlueAgent is stopped at the door because there's no possible way for it to get across this
corridor.
You can Ctrl-D to create a few RedAgents and Ctrl-D to create a
few BlueAgents and watch them bustle around and also hustle past each other to try and get through
the door because the blue guys are going to get in the road of the red.
In this case, they're blocking them completely.
Try and jiggle them around a bit
and now one guy got through. So they might even move just a little bit because they still do jitter around
the spot that they're on and some of them will get through.
But you can see in this the behavior of the red and the blue.
Now if the blue needed to get inside, then we need to create another path for them. To create a path for
the BlueAgents,
I'm going to put a ramp over here.
So what I've done is I've removed the barrels that were around here and I've also gotten rid of the
railings. So there were a couple of railings here and also over on the other side and I've stretched
it out
and rotated it so it's just a stock standard Unity cube, made it bridge across that area.
It's not absolutely perfect and there's a bit of a step there but we are allowing for stepping
up in this navmesh.
I've also reselected a lot of these are other areas in the map to make them into navmesh as
well, so that by the time the agents get over the bridge,
then they can find their way back into that other area.
Now this cube is not attached to the rest of the mesh.
It's by itself and that's fine.
It doesn't matter if it's a part of a whole mesh or a single object, it can still be integrated into the navmesh.
Now we select that particular cube in this case and we go over to our Navigation window and down to
Object.
We make it a Navigation Static, ticking it in there.
We can also tick it in the Inspector and then we set it to 'Walkable', which is what we want it to be.
We go back to Bake and then click on [Bake] and you'll see that you get a nice path going across there.
So let's go back and run this. When I click in here,
the red guys are going to go down the corridor because they can.
The blue guys don't even try to go down the corridor, so it doesn't even come into their calculations as
they head off across the back and then they'll come in and through
this other corridor that's over here and down into this area where the red ones are.
Now the red guys are going to go the shortest path (or the least cost) to get into that area.
Let's just see if we move the red ones over here first, if we can get them to go across the bridge.
I'm just realigning that and I'll set my camera up...Align With View, so we can see more of the station and
then we press Play and I click over here and they'll all go over there because that is the shortest path to get
to there.
Then if I click there, they will all head off across the bridge and then once they're over here, it should
be shorter for them to come inside this way.
Yes.
So they'll come in through there. So in this case, we made them go over there and then it was a shorter distance
for them to come this way than to go back and across through that corridor.
But if they're standing here, at the Start, then the corridor is the quickest way for them to get inside.
How could we make the corridor be more costly for them to go across? Let's say the
corridor is on fire and it would make it a better idea for them to go around that way, than
to come through here.
What you do is increase the cost of this part of the path
and we can do that in the Navigation window. If we go into Areas, 'Corridor' is currently set to a cost of 1,
but if we made that a cost of 10 to go along that corridor, it's going to make the calculation a little
bit different for them.
Once you've changed the cost, you'll need to go back through and rebake it,
so those values get saved into the navmesh.
Ok.
So let's press Play and see what we get now. So let's try and get them to go here.
Now in this case, that must be very borderline that cost of 10 for them to come down because you can see
2 of them who are close enough in vicinity go across through here, got there first, whereas the ones
that were pushed by the blue guys further away from the corridor decided that it was easier to go around
that way.
So in this lecture, we've looked at creating different areas and assigning different areas to different
objects that are in our navmesh and then assigning the different areas into the agent's own pathfinding
system, which is in the navmesh agent by turning them on or off.