So far, we've looked at waypoints.
We looked at waypoints and NAV measures.
Now we're actually going to look at NAV measures in a lot more detail.
And yes, we're going to do it in Unity's Viking Village Asset.
Now you don't have to go and find it in the asset store.
I've got it as a starter file for this lecture, along with some little friends that we're going to
be using in our scenes, namely Chopper here, who's an animated character, but he's going to become
our nav mesh agent, and then we can create a whole flock of choppers down the track.
So when you download the starter package, you're going to find that there's a nav mesh starter scene
in the asset folder.
Just open that up and this is what you're going to find.
The first thing that we need to do is make our nav mesh.
So if we just zoom out a little bit and have a look at what we're dealing with, the Viking Village,
if you haven't actually looked at it before, is made of a few different measures.
So there's these background mountain meshes that are going on up here.
Now, we're not going anywhere near those because you can't actually walk over to there.
We've got this sort of floating plane that's been moulded into a terrain, and then there's a whole
bunch of models on here, like some rocks and houses, planks and what have you.
They are going to be part of this mesh.
So what you need to do is to come over into the hierarchy and locate your environment.
You want to select things like the structures which will include your steps, planks, houses, etc.
down underneath that as well as you've got rocks, you've got the terrain and you've got props.
Now if we just select all of the environment, then come over into the inspector and go to our static
dropdown navigation static.
It'll ask you if you want to do all the children.
Yes, we do.
Now, don't believe that it has actually happened unless you click through and just check that things
have been ticked as navigation static because sometimes I find that it can miss things out.
So we've got that done.
Now that we do, let's open up our navigation tab.
Now mine is sitting behind my inspector.
But if you've forgotten where to get it from, it's window I.
And then navigation.
Once that opens, we're just going to click on the bake tab and hit bake.
Okay, let's select our environment and have a look at the baked areas that we get.
There we go.
Okay.
And that might take a little bit to generate because it is actually quite large.
And you can see I've even selected by complete accident these mountains in the background, which would
have obviously created a lot more for generating the nav mesh.
So if you find it does get a bit clunky, let's find these mountain peaks.
Okay.
They're actually those three models there.
And we will just go over into.
The Inspector again, just tick those ones off as far as navigation static goes.
Then if we go back and bake again, it will actually just leave those out.
So let's go bake this time.
Okay.
Much faster to generate.
And you can see we've got no mesh over there, which is great because we don't need to be over there.
All right.
So there's our nav mesh.
Now, if we have a little bit of a look.
After we've generated it, it'll give you an idea as to how well it has baked things and how well it
is actually made areas where you can't walk.
So if we have a look over here, you can see that the blue areas are where Trump is going to be able
to walk the other bits.
He can't go.
So if we go around this side, the rock, he will actually be able to walk up and go over the top of
that rock.
You might not want him to be able to do that.
So we come back over to our bake tab and find our agent details up here.
What has been baked is the reasonable area for this particular agent here to be able to navigate.
There is a slope at the end, so we're saying 45 degrees in this case.
So it's calculated that this is acceptable to go up and down if you want to reduce that slope so that
chopper can actually go up, things like that.
We can just drag this down, hit bake again.
And now you'll see that he's not going to get up on that rock no matter how hard he tries, because
there's just no path up there.
There's no navigation mesh.
So with that a little bit lower, you sort of restrict where he can go to.
Now down the track, we can do this for all different types of agents, which means that some can get
up on the rock, others can't get up on the rock depending on their different abilities.
Now we can make Chopper into an agent.
So select Chopper in the hierarchy.
Go to the inspector and we're going to add in a nav mesh agent component.
Now, if we just zoom in a little bit, we'll be able to see what that's done to our agent.
So he gets this collider around him.
It's not really a collider in the sense that you might be used to.
It's more of a nav mesh collider, but it's showing you how much room is allocated to him.
So his height is the height of a humanoid because this is the basic humanoid nav mesh agent.
He doesn't need to be this high, okay, because he's only quite low, which means he could actually
go under low things, whereas this height is going to stop him getting through other sort of low areas
if there were any available.
So for example, over there you've got like these gates and pilings.
He might actually be able to duck under there if he's made a bit smaller.
So how do we resize this nav mesh collider around there?
Well, we can go into our nav mesh agent settings and just come down to the obstacle avoidance where
you can change how big he is.
So you can actually bring that height down.
And I'd probably say it's about one might be a little bit lower than that might his eyes might just
poke out the top, but we'll leave it as one.
Okay.
Now we're going to make a controller to run jumper.
So over in your asset, we're going to create a3dc sharp script and we'll call that AI controller and
let's just open that up.
And this is just going to be very, very simple code at the top.
We will need using unity engine dot I and at the top we're here will have a public nav mesh agent agent
and then in the start we will go agent equals this dot get component nav mesh agent.
Okay, that's all we need in here.
So just save that.
Now we go back into unity.
You want to get that code and drag and drop it onto chopper, making sure that it's on the same item
that has the nav mesh agent.
So let's put that there.
Okay.
Now we're going to have lots and lots of agents running around.
So we're going to create an empty game object in the hierarchy and we will call that our agent manager,
then create a new script for that.
So we'll create C-sharp script and we will call that our agent manager and open that up.
Now this is going to collect all of your agents and keep track of them in an array.
So game object array agents.
And then in the start.
Agents is going to equal game objects.
Find game objects with tag.
We'll just do this automatically.
Let's tag them with alien and capitals.
Remind me to do that when we get back into the scene now inside of update to move these guys around,
just want to click on the environment and then get the mouse position from that and then go there.
So we're going to have if input dot get mouse button down zero the left mouse button and then in here
we're going to perform a ray cast.
So we want a ray cast hit.
We'll call that hit and then we'll go if physics dot ray cast camera dot main screen point to ray input
dot mouse position.
So this is actually how you do a ray cast from the mouse position where you've clicked on the screen
out into the environment to find out where we actually hit something.
So we'll have out hit will project that ray for a distance of 100.
Hopefully we'll have hit something within that time or within that distance and then we'll go for each
game, object A in agents, and then we'll just tell them to go to that location.
So I get component I controller dot agent dot set destination hit dot point.
Okay, now this get component isn't particularly a good idea having inside of the update and will come
and sort of rejig that in a moment.
But first just save this.
We have to go back and tag him as an AI.
So we're going to grab Chopper and then over in the inspector, you want to create a new tag for AI,
which will be in capital.
So I save that and go back to Chopper and we will tag that as I like that.
Now with the terrain, I'm just going to see if we have a collider on here because if we don't have
a collider on the actual train itself, we won't be able to click.
But we do.
There's a train collider.
Also select your agent manager and then put the agent manager code on there so that it actually runs.
All right, so let's press play.
What I've done here is actually align my camera up a little bit.
So we're looking down here and if I click, you can now see that he's going to move.
He's not animated.
We have not programmed him to animate in a walking fashion, I should say.
He's just sort of stuck in his idol.
So you can see that he will move from position to position.
Okay, great.
Well, that's the start of our nav mesh agent and our agent.
Let me come back in the next video.
We'll keep improving on this.
We'll fix up that get component thing I was talking about and also add in some proper animations and
some more agents.