WEBVTT

00:02.250 --> 00:06.890
The previous lecture, we finished setting up our graph and our algorithm.

00:06.900 --> 00:13.890
Now we're going to put our waypoints and the edges for those waypoints into our inspector.

00:13.920 --> 00:20.160
First of all, we need to go back to our map that we created and take a look at where all the edges

00:20.160 --> 00:21.230
are between the waypoints.

00:21.240 --> 00:27.600
Now, because I copy and paste in my waypoints instead of dragging the prefab onto the map each time

00:27.600 --> 00:34.800
I had to rename them in the hierarchy manually, which means they're not in any particular logical order.

00:34.800 --> 00:37.860
But that's okay, just so that we know where they are.

00:37.860 --> 00:44.520
And so what I've done here is just listed out what those edges are between each of the waypoints.

00:44.520 --> 00:51.330
Now you'll see that I've got them as a multi-directional waypoint so that you can go both ways, which

00:51.330 --> 00:57.510
means you don't have to sort of list them twice with, for example, 1 to 8 and then 8 to 1.

00:57.510 --> 01:00.150
Because remember in the code we put it.

01:00.150 --> 01:05.280
So if it's multidirectional or bi directional, then it will actually put it in there twice and reverse

01:05.280 --> 01:06.930
those nodes around.

01:07.620 --> 01:12.810
Okay, so I'll show you how to get started putting these in because your waypoints might be slightly

01:12.810 --> 01:15.930
different to mine, so you'll have them in a different order.

01:15.930 --> 01:21.240
But you want to go to your waypoint manager and find where you've got your waypoints and your links

01:21.240 --> 01:23.010
exposed in the inspector.

01:23.040 --> 01:27.810
Now make sure that you click on the padlock to lock that so that we can copy these across.

01:27.810 --> 01:29.250
The waypoints are the easy bit.

01:29.250 --> 01:36.570
You just shift, select all of those and drag them on to waypoints and then drop them there and this

01:36.570 --> 01:40.230
will just automatically populate them for you.

01:40.320 --> 01:40.770
Okay.

01:40.770 --> 01:45.840
The order that they're in here actually doesn't matter whatsoever because these numbers, as I said,

01:45.840 --> 01:48.210
they're just for us so that we know where things are.

01:48.420 --> 01:52.440
Now, as for the links, these might be different to mine.

01:52.440 --> 01:53.640
What you've got in yours.

01:53.640 --> 01:55.860
Let me go through the process of adding just one link.

01:55.860 --> 01:58.890
So if we hit the plus key, we'll have this element zero.

01:58.890 --> 01:59.670
Drop that down.

01:59.670 --> 02:03.120
You'll see that there's a node one and a node two.

02:03.120 --> 02:08.370
Now, if you remember, this is the little structure that we created in our code.

02:08.370 --> 02:11.160
So let's complete this for one thing.

02:11.160 --> 02:19.710
So it's waypoint one to Waypoint eight is that first link that I've got right over near the helipad,

02:19.710 --> 02:24.630
which is this one here, and it's going to go in both directions.

02:24.630 --> 02:26.220
Okay, so it goes that way.

02:26.250 --> 02:30.930
Then we hit plus again and then we go and put this element in.

02:30.930 --> 02:34.080
Now it's going to copy it to be exactly the same as this one.

02:34.620 --> 02:35.190
That's alright.

02:35.190 --> 02:37.230
You're just going to empty them out anyway.

02:37.230 --> 02:37.470
Okay.

02:37.470 --> 02:42.930
I'm going to go through the process now of putting all of these in and you can do yours too.

02:42.930 --> 02:45.420
So I'll just speed up the video to get us past this.

02:49.040 --> 02:50.340
All right, that's done.

02:50.340 --> 02:57.600
Now we can program our AI code for our little green team to make it move on command between these buttons.

02:57.600 --> 03:04.530
So let's go back up into the assets folder and we're going to create a new C-sharp script.

03:04.530 --> 03:09.090
And this C script will call follow waypoints.

03:09.900 --> 03:13.380
Okay, we're going to get that and drag it on to the tank.

03:13.380 --> 03:18.600
So we're going to grab that and drag and drop it onto the tank.

03:19.020 --> 03:25.620
So on the tank, let's click that lock that was on the WP major.

03:25.620 --> 03:31.260
So here's our script here and now let's open it up and start adding in some code.

03:31.380 --> 03:31.890
Okay?

03:31.890 --> 03:39.090
So leave it as a mono behavior because it does need to run like that because unity will be running it.

03:39.090 --> 03:41.940
Let's go transform goal.

03:42.720 --> 03:50.070
So this will be the transform of the goal so that we can look and move towards the goal.

03:50.070 --> 03:55.260
Float speed equals 5.0.

03:55.260 --> 04:02.310
If of course you can change these, if you want the tank to go faster, we want an accuracy amount equals

04:02.310 --> 04:10.620
1.00.0 f float rot speed equals 2.0 F.

04:10.620 --> 04:15.210
Now this should be familiar sort of code because you've used it for following the waypoints when you

04:15.210 --> 04:19.380
had all of those tanks going around the palm trees previously.

04:19.590 --> 04:24.060
So public game object manager.

04:24.060 --> 04:28.920
So this is our waypoint manager that will do all of the graph processing for us.

04:29.280 --> 04:41.250
Game object waypoints to make the tank familiar with all of the waypoints game object current node which

04:41.250 --> 04:48.450
will be storing where it's currently heading towards or currently is on the map.

04:48.810 --> 04:57.180
INT Current waypoint equals zero and that will be the index of the current waypoint through the path

04:57.180 --> 04:59.400
that is being calculated for it.

04:59.430 --> 04:59.850
Okay.

04:59.850 --> 05:01.590
And then finally we'll have a.

05:01.720 --> 05:03.520
Graph G.

05:04.940 --> 05:20.150
So in start, let's set this up with WP s waypoint system equals WP Manager Target component, WP Manager

05:21.320 --> 05:35.690
DOT Waypoints and then G is going to equal the WP manager get component WP Manager graph and then the

05:35.690 --> 05:42.580
current node equals WP SW and then you need an index number.

05:42.590 --> 05:48.140
Now this is going to depend where your tank is starting from.

05:48.170 --> 05:54.740
Okay, now you could calculate it, but instead we're just going to set it at that position.

05:54.740 --> 05:58.110
So let's just flick back into our map.

05:58.130 --> 06:00.740
Now a tank is over near eight.

06:00.800 --> 06:07.760
What I'm going to do just to make things a lot easier is to move the tank so that it is over near Waypoint

06:07.760 --> 06:08.600
one.

06:08.870 --> 06:15.350
So just grab the tank, the W key and move it back over to here.

06:15.380 --> 06:16.560
Now you can put it anywhere.

06:16.580 --> 06:18.420
Of course, you don't have to put it at number one.

06:18.440 --> 06:24.710
Now, number one in the array in our WP manager is at element zero.

06:25.220 --> 06:27.180
So that's the value that you want.

06:27.200 --> 06:35.390
So back in our follow waypoints, we'll put a zero in there for the starting point of our tank.

06:35.660 --> 06:36.110
Okay.

06:36.110 --> 06:43.730
Now, once we've done that, we can create a whole bunch of directives for that tank.

06:43.730 --> 06:52.850
So let's go down here and we'll go public void, go to Heli, which is go to the helipad where we already

06:52.850 --> 06:53.420
are.

06:53.690 --> 06:54.410
Like this.

06:54.410 --> 06:57.380
And this will be g dot a star.

07:00.040 --> 07:10.960
From our current node to WP s zero, which is where the helipad is, where we're actually starting from,

07:10.960 --> 07:12.760
and then we'll set current.

07:14.410 --> 07:18.310
Waypoint to equal zero.

07:18.340 --> 07:24.610
Now, this is not counting the indexes of the waypoints in that array.

07:24.640 --> 07:32.050
When you're given a path of nodes to walk through, current waypoint is picking or pointing to which

07:32.050 --> 07:36.910
node you're currently up to as you're moving through the path.

07:37.180 --> 07:44.650
So the path might only have three nodes in it, and therefore this will just count up from 012.

07:44.650 --> 07:48.340
And then it says, Oh, you're at your destination in that particular path.

07:48.580 --> 07:53.680
Okay, so we've got that list now put in public void.

07:53.770 --> 07:56.590
Go to ruin.

07:56.920 --> 08:03.610
And I'd like you to fill this out as a challenge now what I call the ruin on the map.

08:03.610 --> 08:06.220
If you're not familiar with that, let me just go back in here.

08:06.430 --> 08:10.210
I call this over here.

08:10.310 --> 08:13.000
Okay, so this is kind of ruin.

08:13.000 --> 08:16.540
It's the leftovers of a building, and there's sort of a dead tank there.

08:16.580 --> 08:21.490
Okay, so that's where we want to go to in that particular method, right?

08:21.490 --> 08:24.130
So pause the video now and give that a go.

08:24.130 --> 08:27.310
And when we come back, I'll continue typing that up for you.

08:28.240 --> 08:28.720
Okay.

08:28.720 --> 08:29.620
Welcome back.

08:29.620 --> 08:32.240
How did you go with putting that code together?

08:32.260 --> 08:37.090
Well, first of all, you need to know which waypoint we're going for, which is number two up in this

08:37.090 --> 08:40.150
back corner, or at least it's two in my map.

08:40.150 --> 08:41.800
It might be a different one for you.

08:41.800 --> 08:49.090
Now, the index of that waypoint in our waypoint manager will be number one because this is zero and

08:49.090 --> 08:51.370
then this is going to be one here, right?

08:51.370 --> 08:53.830
So it's going to be one that we're heading towards.

08:53.830 --> 08:56.440
Let's go back into follow waypoints.

08:56.440 --> 09:04.480
So we basically want to grab this code, copy it and put it down into here and set that to waypoint

09:04.570 --> 09:05.530
one.

09:05.590 --> 09:06.000
Okay.

09:06.040 --> 09:07.090
How did you go with that?

09:07.090 --> 09:13.240
Now we're setting it back to zero again because that means we're starting out on a new path.

09:13.630 --> 09:14.140
Okay.

09:14.140 --> 09:15.700
So we're going to leave this video here.

09:15.700 --> 09:23.980
When we come back, we will put together the code to move from a starting waypoint to an ending waypoint

09:23.980 --> 09:29.020
along that path that is calculated in the graph script.
