WEBVTT

00:06.420 --> 00:08.340
So far, we've looked at waypoints.

00:08.340 --> 00:10.670
We looked at waypoints and NAV measures.

00:10.680 --> 00:15.720
Now we're actually going to look at NAV measures in a lot more detail.

00:15.720 --> 00:20.440
And yes, we're going to do it in Unity's Viking Village Asset.

00:20.460 --> 00:23.420
Now you don't have to go and find it in the asset store.

00:23.430 --> 00:29.520
I've got it as a starter file for this lecture, along with some little friends that we're going to

00:29.520 --> 00:36.030
be using in our scenes, namely Chopper here, who's an animated character, but he's going to become

00:36.030 --> 00:41.820
our nav mesh agent, and then we can create a whole flock of choppers down the track.

00:41.820 --> 00:47.250
So when you download the starter package, you're going to find that there's a nav mesh starter scene

00:47.250 --> 00:48.690
in the asset folder.

00:48.720 --> 00:53.730
Just open that up and this is what you're going to find.

00:53.940 --> 00:58.350
The first thing that we need to do is make our nav mesh.

00:58.350 --> 01:04.770
So if we just zoom out a little bit and have a look at what we're dealing with, the Viking Village,

01:04.770 --> 01:09.030
if you haven't actually looked at it before, is made of a few different measures.

01:09.030 --> 01:12.000
So there's these background mountain meshes that are going on up here.

01:12.000 --> 01:16.230
Now, we're not going anywhere near those because you can't actually walk over to there.

01:16.230 --> 01:21.990
We've got this sort of floating plane that's been moulded into a terrain, and then there's a whole

01:21.990 --> 01:27.930
bunch of models on here, like some rocks and houses, planks and what have you.

01:27.930 --> 01:30.780
They are going to be part of this mesh.

01:30.870 --> 01:38.250
So what you need to do is to come over into the hierarchy and locate your environment.

01:38.280 --> 01:46.890
You want to select things like the structures which will include your steps, planks, houses, etc.

01:46.890 --> 01:52.290
down underneath that as well as you've got rocks, you've got the terrain and you've got props.

01:52.470 --> 02:02.550
Now if we just select all of the environment, then come over into the inspector and go to our static

02:02.550 --> 02:04.830
dropdown navigation static.

02:04.830 --> 02:07.140
It'll ask you if you want to do all the children.

02:07.140 --> 02:08.310
Yes, we do.

02:08.340 --> 02:15.450
Now, don't believe that it has actually happened unless you click through and just check that things

02:15.450 --> 02:23.190
have been ticked as navigation static because sometimes I find that it can miss things out.

02:23.310 --> 02:25.260
So we've got that done.

02:25.260 --> 02:28.500
Now that we do, let's open up our navigation tab.

02:28.500 --> 02:31.050
Now mine is sitting behind my inspector.

02:31.050 --> 02:35.250
But if you've forgotten where to get it from, it's window I.

02:35.250 --> 02:36.840
And then navigation.

02:37.260 --> 02:43.740
Once that opens, we're just going to click on the bake tab and hit bake.

02:43.950 --> 02:49.710
Okay, let's select our environment and have a look at the baked areas that we get.

02:49.740 --> 02:50.610
There we go.

02:50.820 --> 02:51.220
Okay.

02:51.220 --> 02:54.180
And that might take a little bit to generate because it is actually quite large.

02:54.180 --> 03:00.360
And you can see I've even selected by complete accident these mountains in the background, which would

03:00.360 --> 03:04.530
have obviously created a lot more for generating the nav mesh.

03:04.530 --> 03:08.100
So if you find it does get a bit clunky, let's find these mountain peaks.

03:08.100 --> 03:08.280
Okay.

03:08.280 --> 03:10.380
They're actually those three models there.

03:10.410 --> 03:13.620
And we will just go over into.

03:14.620 --> 03:21.430
The Inspector again, just tick those ones off as far as navigation static goes.

03:21.640 --> 03:26.590
Then if we go back and bake again, it will actually just leave those out.

03:26.590 --> 03:28.480
So let's go bake this time.

03:28.650 --> 03:29.020
Okay.

03:29.050 --> 03:30.190
Much faster to generate.

03:30.190 --> 03:35.650
And you can see we've got no mesh over there, which is great because we don't need to be over there.

03:35.860 --> 03:36.460
All right.

03:36.460 --> 03:37.740
So there's our nav mesh.

03:37.750 --> 03:39.790
Now, if we have a little bit of a look.

03:39.820 --> 03:46.030
After we've generated it, it'll give you an idea as to how well it has baked things and how well it

03:46.030 --> 03:49.930
is actually made areas where you can't walk.

03:49.930 --> 03:56.830
So if we have a look over here, you can see that the blue areas are where Trump is going to be able

03:56.830 --> 03:58.840
to walk the other bits.

03:58.840 --> 03:59.830
He can't go.

04:00.070 --> 04:06.550
So if we go around this side, the rock, he will actually be able to walk up and go over the top of

04:06.550 --> 04:07.330
that rock.

04:07.390 --> 04:10.780
You might not want him to be able to do that.

04:11.050 --> 04:17.620
So we come back over to our bake tab and find our agent details up here.

04:17.950 --> 04:25.450
What has been baked is the reasonable area for this particular agent here to be able to navigate.

04:26.200 --> 04:30.670
There is a slope at the end, so we're saying 45 degrees in this case.

04:30.670 --> 04:37.690
So it's calculated that this is acceptable to go up and down if you want to reduce that slope so that

04:37.690 --> 04:40.150
chopper can actually go up, things like that.

04:40.150 --> 04:42.940
We can just drag this down, hit bake again.

04:43.090 --> 04:48.550
And now you'll see that he's not going to get up on that rock no matter how hard he tries, because

04:48.550 --> 04:50.590
there's just no path up there.

04:50.590 --> 04:52.510
There's no navigation mesh.

04:53.020 --> 04:58.420
So with that a little bit lower, you sort of restrict where he can go to.

04:59.200 --> 05:04.960
Now down the track, we can do this for all different types of agents, which means that some can get

05:04.960 --> 05:09.520
up on the rock, others can't get up on the rock depending on their different abilities.

05:09.760 --> 05:13.810
Now we can make Chopper into an agent.

05:13.810 --> 05:16.000
So select Chopper in the hierarchy.

05:16.150 --> 05:21.760
Go to the inspector and we're going to add in a nav mesh agent component.

05:22.330 --> 05:27.520
Now, if we just zoom in a little bit, we'll be able to see what that's done to our agent.

05:27.520 --> 05:31.120
So he gets this collider around him.

05:31.120 --> 05:34.300
It's not really a collider in the sense that you might be used to.

05:34.300 --> 05:41.740
It's more of a nav mesh collider, but it's showing you how much room is allocated to him.

05:41.740 --> 05:48.550
So his height is the height of a humanoid because this is the basic humanoid nav mesh agent.

05:48.970 --> 05:54.460
He doesn't need to be this high, okay, because he's only quite low, which means he could actually

05:54.460 --> 06:01.780
go under low things, whereas this height is going to stop him getting through other sort of low areas

06:01.780 --> 06:03.910
if there were any available.

06:03.910 --> 06:07.900
So for example, over there you've got like these gates and pilings.

06:07.900 --> 06:11.350
He might actually be able to duck under there if he's made a bit smaller.

06:11.350 --> 06:16.360
So how do we resize this nav mesh collider around there?

06:16.360 --> 06:22.450
Well, we can go into our nav mesh agent settings and just come down to the obstacle avoidance where

06:22.450 --> 06:25.150
you can change how big he is.

06:25.150 --> 06:27.580
So you can actually bring that height down.

06:27.580 --> 06:33.730
And I'd probably say it's about one might be a little bit lower than that might his eyes might just

06:33.730 --> 06:36.100
poke out the top, but we'll leave it as one.

06:36.100 --> 06:36.490
Okay.

06:36.490 --> 06:39.700
Now we're going to make a controller to run jumper.

06:39.700 --> 06:48.670
So over in your asset, we're going to create a3dc sharp script and we'll call that AI controller and

06:48.670 --> 06:50.200
let's just open that up.

06:50.290 --> 06:54.040
And this is just going to be very, very simple code at the top.

06:54.040 --> 07:07.510
We will need using unity engine dot I and at the top we're here will have a public nav mesh agent agent

07:08.320 --> 07:19.600
and then in the start we will go agent equals this dot get component nav mesh agent.

07:21.370 --> 07:23.500
Okay, that's all we need in here.

07:23.500 --> 07:24.700
So just save that.

07:24.700 --> 07:26.830
Now we go back into unity.

07:26.830 --> 07:34.900
You want to get that code and drag and drop it onto chopper, making sure that it's on the same item

07:34.900 --> 07:36.250
that has the nav mesh agent.

07:36.250 --> 07:38.320
So let's put that there.

07:38.620 --> 07:39.040
Okay.

07:39.040 --> 07:42.490
Now we're going to have lots and lots of agents running around.

07:42.490 --> 07:50.800
So we're going to create an empty game object in the hierarchy and we will call that our agent manager,

07:52.000 --> 07:53.980
then create a new script for that.

07:53.980 --> 08:01.810
So we'll create C-sharp script and we will call that our agent manager and open that up.

08:01.810 --> 08:07.030
Now this is going to collect all of your agents and keep track of them in an array.

08:07.030 --> 08:11.290
So game object array agents.

08:12.160 --> 08:14.110
And then in the start.

08:14.170 --> 08:17.650
Agents is going to equal game objects.

08:18.190 --> 08:21.070
Find game objects with tag.

08:21.340 --> 08:23.320
We'll just do this automatically.

08:23.320 --> 08:25.750
Let's tag them with alien and capitals.

08:26.320 --> 08:32.920
Remind me to do that when we get back into the scene now inside of update to move these guys around,

08:32.930 --> 08:37.990
just want to click on the environment and then get the mouse position from that and then go there.

08:38.290 --> 08:50.380
So we're going to have if input dot get mouse button down zero the left mouse button and then in here

08:50.620 --> 08:53.050
we're going to perform a ray cast.

08:53.620 --> 08:56.230
So we want a ray cast hit.

08:57.370 --> 09:10.090
We'll call that hit and then we'll go if physics dot ray cast camera dot main screen point to ray input

09:10.610 --> 09:11.860
dot mouse position.

09:11.860 --> 09:17.080
So this is actually how you do a ray cast from the mouse position where you've clicked on the screen

09:17.080 --> 09:20.650
out into the environment to find out where we actually hit something.

09:20.650 --> 09:25.570
So we'll have out hit will project that ray for a distance of 100.

09:25.570 --> 09:31.690
Hopefully we'll have hit something within that time or within that distance and then we'll go for each

09:32.020 --> 09:39.760
game, object A in agents, and then we'll just tell them to go to that location.

09:39.760 --> 09:53.260
So I get component I controller dot agent dot set destination hit dot point.

09:53.380 --> 09:59.470
Okay, now this get component isn't particularly a good idea having inside of the update and will come

09:59.470 --> 10:02.020
and sort of rejig that in a moment.

10:02.020 --> 10:03.700
But first just save this.

10:04.000 --> 10:08.230
We have to go back and tag him as an AI.

10:08.440 --> 10:17.260
So we're going to grab Chopper and then over in the inspector, you want to create a new tag for AI,

10:17.410 --> 10:19.180
which will be in capital.

10:19.180 --> 10:26.980
So I save that and go back to Chopper and we will tag that as I like that.

10:27.010 --> 10:31.630
Now with the terrain, I'm just going to see if we have a collider on here because if we don't have

10:31.630 --> 10:35.440
a collider on the actual train itself, we won't be able to click.

10:35.440 --> 10:36.070
But we do.

10:36.280 --> 10:37.540
There's a train collider.

10:37.780 --> 10:44.290
Also select your agent manager and then put the agent manager code on there so that it actually runs.

10:44.290 --> 10:46.390
All right, so let's press play.

10:46.390 --> 10:49.390
What I've done here is actually align my camera up a little bit.

10:49.390 --> 10:55.420
So we're looking down here and if I click, you can now see that he's going to move.

10:55.420 --> 10:56.380
He's not animated.

10:56.380 --> 11:00.880
We have not programmed him to animate in a walking fashion, I should say.

11:00.880 --> 11:03.190
He's just sort of stuck in his idol.

11:03.610 --> 11:08.620
So you can see that he will move from position to position.

11:09.460 --> 11:10.750
Okay, great.

11:10.750 --> 11:15.820
Well, that's the start of our nav mesh agent and our agent.

11:16.000 --> 11:17.560
Let me come back in the next video.

11:17.560 --> 11:18.880
We'll keep improving on this.

11:18.880 --> 11:24.220
We'll fix up that get component thing I was talking about and also add in some proper animations and

11:24.220 --> 11:25.630
some more agents.
