WEBVTT

00:03.190 --> 00:08.710
In this lecture, we will start looking at one of the most fundamental AI techniques used in navigation

00:08.860 --> 00:09.910
waypoints.

00:10.110 --> 00:15.190
Waypoints are simply positions on a map that, when navigated in sequence, produce a path.

00:15.460 --> 00:20.410
It's the same system used in a car GPS that gives you instructions on where to drive.

00:20.800 --> 00:26.110
Waypoints are an easy system to implement in an NPC as it makes them look more intelligent than they

00:26.110 --> 00:26.650
are.

00:26.800 --> 00:33.490
And by providing them with a safe set of locations on a map to move between, they can also avoid obstacles.

00:33.730 --> 00:37.450
We've already created an embassy that can move and follow a goal location.

00:37.450 --> 00:40.360
Updating this to a waypoint system is straightforward.

00:40.930 --> 00:46.000
In this video, we're going to put together a waypoint system and have these tanks drive around from

00:46.000 --> 00:48.010
one to the other in a circuit.

00:48.130 --> 00:52.570
To begin with, you need the starter resource that's attached to this lecture.

00:52.570 --> 00:58.180
So download that open up a new unity project and bring those resources in.

00:58.330 --> 00:59.320
Inside of it.

00:59.320 --> 01:04.150
You're going to find a scene called Tank Racing, and if you open up that scene, you'll get something

01:04.150 --> 01:04.630
like this.

01:04.630 --> 01:09.880
So we have a ground which is made of a cube that is stretched out with a texture on it.

01:09.880 --> 01:17.410
There's four different colored tanks and there's a palm tree, the palm tree we will use for our waypoints.

01:17.950 --> 01:23.020
Essentially, a waypoint only needs to be a 3D position or 2D.

01:23.020 --> 01:26.860
If you're on a 2D map, that's somewhere in your game environment.

01:26.860 --> 01:34.780
Usually when I'm putting waypoints in for, say, a racing track or inside of a game map, then I just

01:34.780 --> 01:40.330
use the sphere or a cube in unity just so I can see where it is.

01:40.330 --> 01:45.040
And then later on you can remove its renderer and make sure, of course, if you've got waypoints,

01:45.040 --> 01:49.270
that you do take off any colliders because they're not really in the environment.

01:49.270 --> 01:55.150
They're just showing us the positions where our AI controlled characters should go between.

01:55.270 --> 01:59.410
But in this case, I'm using a palm tree so we can actually see it.

01:59.410 --> 02:02.800
And in this kind of desert scene, it kind of makes sense.

02:02.800 --> 02:10.930
I guess with your palm tree, what you need to do is to control DX to duplicate it, then hit your w

02:10.930 --> 02:14.440
key and possibly in a top down view.

02:14.470 --> 02:20.890
Just move that palm tree, try and keep it on the plane, but just move it.

02:20.890 --> 02:24.220
And what we're going to do is just make a bit of a circuit with it.

02:24.340 --> 02:30.430
Control D again, grab the duplicate and then Control D.

02:31.730 --> 02:33.230
Make yourself.

02:35.060 --> 02:38.960
Just a bit of a circular.

02:40.330 --> 02:43.140
Thing that your tank can go around.

02:43.150 --> 02:43.320
Okay.

02:43.330 --> 02:48.880
So this tank is going to go around these and use it kind of like a little bit of a racing track.

02:48.880 --> 02:51.070
And these are the flag points for it.

02:51.370 --> 02:51.760
All right.

02:51.760 --> 02:55.780
So now let's actually write some code to achieve this.

02:55.780 --> 02:59.830
So go create C-sharp down in your assets.

02:59.830 --> 03:03.190
And we're going to call this follow waypoint.

03:03.520 --> 03:08.170
So follow woop and then open that up.

03:08.170 --> 03:13.210
First of all, your script needs to know about the waypoints themselves and we're going to put those

03:13.210 --> 03:14.980
into an array of game objects.

03:14.980 --> 03:21.820
So create a public game object array for your waypoints.

03:23.390 --> 03:28.160
Then we also need an integer to keep track of which waypoint we're on.

03:28.160 --> 03:34.490
So we'll call that current waypoint and we will set that to a value of zero.

03:34.520 --> 03:36.980
Because your array index always starts at zero.

03:36.980 --> 03:39.980
So that very first tree will be the starting point.

03:40.280 --> 03:45.680
Okay, now we have to move our tank around and therefore, while we're up here, let's just create a

03:45.680 --> 03:48.050
public float for speed.

03:48.990 --> 03:54.930
And we will set that to 10.0 F and of course, you can change that however you like.

03:55.080 --> 03:55.410
All right.

03:55.410 --> 04:02.820
We don't need a start at this point, but down in update, we're going to basically loop through all

04:02.820 --> 04:03.660
these waypoints.

04:03.660 --> 04:11.790
But we don't want to go to the next waypoint until we've actually reached the next waypoint.

04:11.940 --> 04:12.870
Does that make sense?

04:12.900 --> 04:13.200
Okay.

04:13.200 --> 04:18.540
So we start at Waypoint zero and then we test if we're close enough to that waypoint and then we go

04:18.540 --> 04:19.830
to the next waypoint.

04:19.860 --> 04:27.150
Now, do you remember how we test to find out if our character is actually near its goal location?

04:27.150 --> 04:30.330
Because this is what we need to do for each waypoint.

04:30.630 --> 04:32.580
So we have to use a distance.

04:32.580 --> 04:35.790
And in here I'm going to put if vector three.

04:36.400 --> 04:42.220
Dot distance, so we'll use Unity's Maths Library rather than writing our own again.

04:42.220 --> 04:48.010
And we're going to find the distance between this dot transform dot position.

04:48.010 --> 04:52.270
So that's going to be the tank and the waypoints.

04:53.490 --> 04:55.590
Current white point.

04:55.830 --> 04:59.110
So this is the first waypoint when we first come in here.

04:59.380 --> 05:01.650
Transform position.

05:03.210 --> 05:09.300
And then we need to say, well, when are we close enough to that particular waypoint?

05:09.570 --> 05:16.290
So let's say if it's less than three, we will consider that we've actually reached that waypoint.

05:16.290 --> 05:22.530
And when we do, we're going to update what current waypoint is, current waypoint plus plus.

05:22.530 --> 05:27.700
So then current waypoint will be one and therefore we can start heading towards that.

05:27.720 --> 05:32.730
Now at some point our waypoints are going to run out.

05:33.000 --> 05:34.980
So say we've got ten waypoints.

05:34.980 --> 05:41.190
At some point, current waypoint is going to be an index value of ten or greater, which won't exist

05:41.190 --> 05:43.200
inside of this array.

05:43.200 --> 05:48.180
Because if you've got ten waypoints, then you only have indexes of 0 to 9.

05:48.360 --> 05:53.130
Now when that happens, we need to actually switch our current waypoint back to the first one.

05:53.130 --> 06:01.590
So if current waypoint is greater than or equal to our waypoints length.

06:03.310 --> 06:06.520
Then we're going to go current waypoint

06:08.200 --> 06:12.760
equals zero and we're also going to spell it correctly.

06:12.860 --> 06:15.130
Okay, so there's our current waypoint.

06:15.130 --> 06:20.590
So now we've taken care of going to the next waypoint and also going back to the zero waypoint if we

06:20.640 --> 06:21.440
reached the end.

06:21.460 --> 06:25.360
The next thing we really need to be doing is actually pushing our tank forward.

06:25.360 --> 06:33.310
So we're going to grow this dot, transform dot, look at and remember what it does.

06:33.430 --> 06:37.060
It's going to turn your character to look at another location.

06:37.060 --> 06:41.620
So in this case, we want to look at the next waypoint or look at the waypoint we're going towards.

06:41.710 --> 06:47.710
So waypoints current waypoint dot transform

06:49.870 --> 07:00.220
and then this dot transform dot trends late zero zero.

07:00.220 --> 07:07.960
So nothing in the X and the Y directions, but we want to go along the z at speed multiplied by time.

07:09.600 --> 07:13.170
Dot Delta time because we're inside of the update.

07:13.680 --> 07:14.220
All right.

07:14.220 --> 07:16.140
So save that.

07:16.260 --> 07:20.760
Go back into unity and we need to put this onto a tank.

07:20.760 --> 07:22.290
So we're just going to start with one tank.

07:22.290 --> 07:25.710
So select your green tank or whichever one you want.

07:25.740 --> 07:29.610
Drag and drop that follow waypoint script onto there.

07:29.850 --> 07:30.150
Okay.

07:30.150 --> 07:34.320
We now have our waypoints array at the top here that we can put all of these palm trees in.

07:34.350 --> 07:38.190
Now you can put them in one by one, or you can select the whole lot of them.

07:38.220 --> 07:45.630
If you created them in a circuit in the order of your circuit, then it's going to be much easier just

07:45.630 --> 07:51.120
to drag and drop the whole lot in and then they'll end up in the right order in the waypoints array.

07:51.150 --> 07:57.240
So just go up to the top and click on the little lock which will lock this inspector view.

07:57.270 --> 08:03.360
Then over in the hierarchy you can select the first palm tree shift, select the last palm tree, and

08:03.360 --> 08:10.120
just drag and drop those on to waypoints, and that will populate that whole array in the correct order.

08:10.140 --> 08:12.510
Remember, we're going from one waypoint to the other.

08:12.510 --> 08:18.450
So if you change these ordering around, that will be the way that the tank will go.

08:18.690 --> 08:24.120
Okay, so let's just remember to unlock our inspector view and now we're ready to try this out.

08:24.120 --> 08:32.130
So press play and we will see that our tank goes off around the waypoints.

08:38.840 --> 08:39.200
Okay.

08:39.200 --> 08:44.870
So with our current setup, we've got our look at which is making our tank snap around to look at the

08:44.870 --> 08:45.540
new waypoints.

08:45.560 --> 08:50.300
Now, that's quite an unnatural looking way for a character to move.

08:50.300 --> 08:56.480
We actually want it to sort of turn more slowly and more naturally towards the next waypoint.

08:56.810 --> 09:00.320
So in the next video, we'll come back and deal with that.
