WEBVTT

00:02.550 --> 00:08.940
In this lecture, we're going to look at creating a waypoint system that can be navigated with the aStar

00:08.940 --> 00:14.700
algorithm to begin download the resource file that comes with this lecture.

00:14.850 --> 00:18.080
And in it you'll find this scene with the tank.

00:18.090 --> 00:23.850
If you've done any of the other unity provided tutorials, you'll have seen these models before, and

00:23.850 --> 00:29.550
I use them quite often because while they're nice to look at and they're convenient and easy, so we

00:29.570 --> 00:31.020
don't put our own map together.

00:31.020 --> 00:34.320
Of course, if you want to put your own map together, go for it.

00:34.320 --> 00:35.160
That's fine.

00:35.310 --> 00:38.820
Now we've got a map and we've also got a little tank.

00:38.820 --> 00:42.210
The tank will be our NPC character.

00:42.360 --> 00:49.170
What we're going to do is place some white points around the map and then we're going to link them up

00:49.170 --> 00:56.100
with edges and put some buttons on the screen to give the tank orders of where to go to.

00:56.640 --> 00:59.540
The first thing we'll do is place some waypoints.

00:59.550 --> 01:05.970
The important thing to remember here is that there's no physics or collision detection that will be

01:05.970 --> 01:12.090
in this project, which means you have to place waypoints where the tank can move.

01:12.090 --> 01:16.770
So it'll be the paths in the project.

01:16.770 --> 01:25.140
I've created a waypoint game object asset here, so this prefab, if I drag and drop it into the scene,

01:25.680 --> 01:34.530
you'll see that it automatically gives itself a number and each time you add a waypoint, it will count

01:34.530 --> 01:35.130
up for you.

01:35.130 --> 01:40.800
So this is so you can keep track of which waypoint is which one without having to click on it in the

01:40.800 --> 01:42.570
hierarchy each time.

01:42.960 --> 01:44.600
So let's just zoom out.

01:44.610 --> 01:52.080
I'm going to play some waypoints around this map to get to the helipad, which is down here, and one

01:52.080 --> 01:56.280
to get over here and to go around this building.

01:56.280 --> 02:00.000
But you can play somewhere if you like, so I'll just go ahead and do that quickly.

02:05.960 --> 02:11.600
With all the waypoints in place, they will become the nodes in our graph.

02:11.870 --> 02:16.070
So we need to now determine where the edges are going to be.

02:16.070 --> 02:25.040
And the edges are the lines that connect the nodes and will tell us where the tank can travel between.

02:25.460 --> 02:32.240
As I said at the beginning, these waypoints are also going to specify where the tank can't travel,

02:32.240 --> 02:37.490
and that is that we don't want to put any paths through geometry.

02:37.490 --> 02:41.360
So it's working as a collision avoidance system as well.

02:41.360 --> 02:47.930
And so you can put waypoints like this through buildings if you want your NPCs to travel around in there

02:47.930 --> 02:52.510
and look like they're intelligent because then they're avoiding obstacles.

02:52.520 --> 03:00.410
So in this case we can connect waypoint one with two and two with three and one with three.

03:00.410 --> 03:07.310
Anywhere where there's a clear line of sight, we can join them so we can't get from here over to number

03:07.310 --> 03:09.350
seven at the back going that way.

03:09.350 --> 03:16.760
So we'll put lines here and here and I might even put a one way path that goes up to here so that the

03:16.760 --> 03:22.580
tank, if it is over here and wants to go back, has to go around this way.

03:23.090 --> 03:27.770
With all your paths planned out, you might like to write it down so that you've got a reference for

03:27.770 --> 03:30.620
when we go and put this information back into unity.

03:31.660 --> 03:36.700
Now before you do that, you might want to rename all of your waypoints and number them properly.

03:36.700 --> 03:44.100
So I was doing a duplication of each waypoint and moving around on the map, and that means that unity

03:44.110 --> 03:51.430
gave them odd numbers like WP 003 was actually WP 002 and in brackets one.

03:51.430 --> 03:53.460
And then there was in brackets two, in brackets three.

03:53.470 --> 03:56.110
But instead you might want to number them like this.

03:56.110 --> 03:58.030
So it just seems a little bit more logical.

03:58.030 --> 04:05.440
And then you will see those names properly appear on your map so that you can work out your edge list

04:05.440 --> 04:11.650
between all these according to the graph that you just came up with, with the lines between them.

04:12.280 --> 04:14.620
Now it's time to start creating some code.

04:14.620 --> 04:22.030
But just before we do, we need an object in the scene which is going to manage all the waypoints essentially.

04:22.030 --> 04:26.470
So in the hierarchy, right click and create an empty.

04:26.470 --> 04:31.180
And we're going to call this object a WP manager.

04:32.450 --> 04:35.390
So this is our waypoint manager on that.

04:35.390 --> 04:36.320
We'll put some code.

04:36.320 --> 04:43.880
So let's go over to the project and create a new C-sharp script and we'll also call that WP manager.

04:43.910 --> 04:46.070
It doesn't matter if they're not the same.

04:46.070 --> 04:47.960
I just like to keep it consistent.

04:48.500 --> 04:54.770
Now with that WP manager, I'm going to drag and drop it on to our empty game object.

04:56.180 --> 04:56.540
Right.

04:56.540 --> 04:59.850
So when you've got that code, let's open that up.

04:59.870 --> 05:02.850
Now, let me just make this font a little bit bigger for you.

05:02.870 --> 05:05.140
So I think I turned it down the other day.

05:05.150 --> 05:09.800
Okay, so in here, what we're going to do, first of all, is going to create a little structure at

05:09.800 --> 05:17.060
the top to help us make the inspector look good when we go to link up all of these nodes or waypoints,

05:17.060 --> 05:21.260
I should say, between each other with the edges or the paths.

05:21.260 --> 05:28.760
So let's create a system dot serialize able and then a quick struct.

05:28.760 --> 05:33.530
So public struct and we'll call it link.

05:33.530 --> 05:40.730
So this defines a link as in the edge or the path between two nodes or waypoints.

05:41.120 --> 05:51.890
So in here we will have public enum which will set a direction for and that direction will specify whether

05:51.890 --> 05:55.880
the path is one way or two ways.

05:55.880 --> 05:59.600
Now most of the paths we've put in are two way paths.

05:59.600 --> 06:05.870
In fact, I think all the ones we are, you don't want it to go into sort of a dead end.

06:05.870 --> 06:13.220
If it was to cycle maybe around that factory that's in one part of the map, you maybe you could have

06:13.220 --> 06:19.160
a uni directional path that went around that so that you could only go one way around that.

06:19.700 --> 06:31.220
Okay, let's put public game object node one, public game object node two.

06:32.060 --> 06:39.050
And then we want a public D by R, direction d, i r.

06:39.320 --> 06:45.320
So that's going to hold whether it's a unique or bidirectional link between them.

06:45.590 --> 06:46.040
All right.

06:46.040 --> 06:47.360
So we've done that.

06:47.360 --> 06:52.910
Now, what we're going to do is to put some code down in the actual class itself.

06:53.960 --> 06:58.520
So at the top here, we will create an array of game objects.

06:58.520 --> 07:04.670
So public game object array, which will be our waypoints.

07:05.930 --> 07:12.350
Then we'll have a public link array, which will be our links.

07:13.820 --> 07:14.300
Okay.

07:14.300 --> 07:18.140
Now once we've got this, we can then set up our graph.

07:18.140 --> 07:21.900
Now we want to use an official graph library for this.

07:21.900 --> 07:27.560
So we'll write our own quickly, which means we have to get out of here and start doing that instead.

07:27.560 --> 07:33.200
So we're just going to save this code and I'm going to leave this lecture here so that we can look at

07:33.200 --> 07:40.370
the graph code separately in another set of lectures so that it's sort of one defined area.

07:40.370 --> 07:42.230
And then we'll come back to this.

07:42.230 --> 07:44.420
Okay, so I'll see you in the next lecture.
