WEBVTT

00:06.640 --> 00:12.370
Now, before we can start programming our car to self-drive, it needs to know where it's driving.

00:12.370 --> 00:15.580
So we're actually going to create a circuit using waypoints.

00:15.580 --> 00:23.440
And we should be pretty familiar with waypoints by now because they're used so much in A.I. for our

00:23.440 --> 00:26.140
A.I. agents in games.

00:26.140 --> 00:32.110
So we're going to create a simple script which will define our circuit as well as show you with some

00:32.110 --> 00:33.460
gizmos what it looks like.

00:33.610 --> 00:36.790
Okay, so let's go to create C-sharp sharp script.

00:36.790 --> 00:40.570
Let's call it circuit and we will open that up.

00:40.600 --> 00:41.020
Okay.

00:41.020 --> 00:47.980
The first thing up here will be a public array of game objects, and these game objects will be markers

00:47.980 --> 00:50.410
that show us where our circuit is going.

00:50.500 --> 00:54.940
So we want game object and we'll put waypoints, we'll call it.

00:55.270 --> 00:59.650
This will be slightly different way to the way we did waypoints before, but a lot of it will be familiar

00:59.650 --> 01:01.030
to you, right?

01:01.030 --> 01:05.560
So we don't want any of these things in here.

01:05.560 --> 01:13.450
Let's get rid of these and we're going to create void on draw gizmos.

01:13.720 --> 01:20.380
And this will be and then in here we'll put draw gizmos false.

01:21.070 --> 01:28.990
And then down in here we want void on draw gizmos selected.

01:29.680 --> 01:36.010
And this will allow us to actually just draw the circuit when we've got the circuit clicked.

01:36.010 --> 01:39.610
This up here should actually be draw gizmos, not on draw gizmos.

01:40.090 --> 01:40.420
Okay.

01:40.420 --> 01:44.560
And then in here we want draw gizmos.

01:47.280 --> 01:48.120
True.

01:48.690 --> 01:49.050
Okay.

01:49.080 --> 01:52.790
We haven't written drug gizmos, which is why it keeps flipping back on me.

01:52.800 --> 01:56.340
Now, void draw gizmos.

01:58.020 --> 02:04.620
Bool selected that in here will actually do the drawing.

02:04.620 --> 02:11.640
So if selected equals false return.

02:12.390 --> 02:27.570
If waypoints dot length is greater than one, then vector three previous way point here so previous

02:27.570 --> 02:36.960
or prev equals waypoints zero dot transform dot position and then we're going to loop through the rest

02:36.960 --> 02:37.770
of the waypoint.

02:37.770 --> 02:45.390
So int i equals one is less than waypoints dot length.

02:47.520 --> 02:48.240
I plus.

02:48.240 --> 02:49.040
Plus.

02:49.890 --> 02:50.120
Okay.

02:50.220 --> 02:52.590
And then in here we'll draw the lines between them.

02:52.590 --> 02:59.370
So vector three for the next waypoint will be equal to waypoints.

02:59.580 --> 03:14.850
I transformed position and then gizmos dot draw line between previous and next and then we'll set the

03:14.850 --> 03:20.100
previous to equal the next so that we move on to the next pair basically.

03:20.190 --> 03:29.010
And then at the end of this will go gizmodo's dot draw line from whatever's now the previous to the

03:29.010 --> 03:37.680
first waypoint which will be waypoints zero dot transform dot position.

03:38.520 --> 03:39.240
Okay.

03:39.240 --> 03:44.700
Right so you've got that code now let's save it and we will switch back into unity.

03:44.700 --> 03:49.980
We're going to go into the hierarchy and create a empty game object.

03:49.980 --> 03:59.010
Let's call that circuit and we will attach circuit here to circuit there.

03:59.010 --> 04:01.110
So there's our circuit and is our waypoints.

04:01.110 --> 04:05.460
Now we want to make some waypoints, so we're going to create a cube.

04:06.120 --> 04:07.290
And here's my cube.

04:07.290 --> 04:13.200
Now I want to move that cube down onto the ground.

04:15.190 --> 04:21.850
Like this half way up because it's basically that center point in that cube that is marking where our

04:21.850 --> 04:22.870
waypoint is.

04:22.990 --> 04:29.590
Now, that cube, let's just call it waypoint like that.

04:29.620 --> 04:30.060
Okay.

04:30.070 --> 04:36.430
Now we're going to attach that cube to the circuit just to keep them all nice and cleanly put together.

04:36.580 --> 04:36.920
Okay.

04:36.940 --> 04:41.470
Now, with this waypoint, let's go control D and make another one.

04:41.560 --> 04:49.180
Move it out here and make this quite a big track because the car is not a little car like, you know,

04:49.230 --> 04:53.740
you can't make little tiny turns if the track is really, really tiny.

04:53.740 --> 04:56.590
So let's just make something like this.

04:57.410 --> 05:00.080
Okay, so there's our four waypoints.

05:00.100 --> 05:06.910
Now, if you go back to the circuit in our waypoints, we can actually shift, select all of those.

05:07.000 --> 05:12.460
And for our circuit, in fact, we actually have to lock that first so it doesn't disappear when we

05:12.460 --> 05:15.610
select these drag these onto waypoints.

05:16.240 --> 05:16.450
Okay.

05:16.480 --> 05:18.280
So now they're all in there.

05:18.280 --> 05:23.770
And if we click on circuit, you'll be able to see the circuit that has been created and it's going

05:23.770 --> 05:25.920
to show up like that.

05:25.930 --> 05:34.270
So what that means is if we go into our waypoints and maybe swap waypoint two with Waypoint one and

05:34.270 --> 05:39.850
then click on the circuit, you can see now that this is the circuit and the way that it's going to

05:39.850 --> 05:40.330
go.

05:40.330 --> 05:44.530
But I actually don't want that way because it'll get a bit messy when we start trying to drive around

05:44.530 --> 05:44.620
it.

05:44.620 --> 05:52.540
So let's just put those back in so that we have a nice square round ish kind of circuit for the car

05:52.540 --> 05:53.740
to go between.

05:54.790 --> 06:02.980
So create as many of these waypoints that you like and then hook them up into the circuit and that will

06:02.980 --> 06:05.800
give us a track for the car to follow.

06:05.920 --> 06:06.400
All right.

06:06.400 --> 06:13.750
So in the next video, we're going to come back and start putting in the code for the AI for this car

06:13.750 --> 06:16.990
controller so that it can make its way around these waypoints.
