WEBVTT

00:03.320 --> 00:09.770
In this lecture, we're going to start writing the code to drive the car with a artificial intelligence

00:09.770 --> 00:10.710
controller.

00:10.730 --> 00:17.960
So what we need to consider is the forward direction of the car and the vector to the waypoint.

00:18.080 --> 00:25.850
From that, we can work out the turning angle that the car needs to turn to start facing in the waypoint

00:25.880 --> 00:26.840
direction.

00:26.870 --> 00:32.110
And to do this, we can just use some simple high school trigonometry.

00:32.120 --> 00:38.990
So if you remember your rules about tan, cosine and sine for finding the angles of a right angle triangle,

00:39.020 --> 00:46.430
you can actually construct a right angle triangle like this where you've got your X and Z values, which

00:46.430 --> 00:52.580
is basically the distance between your vehicle and the next waypoint.

00:52.610 --> 01:03.830
Now, using a ten two will allow us to also find the full angle based on the quadrant which the angle

01:03.830 --> 01:05.300
appears in.

01:05.360 --> 01:11.090
Now, I won't go into this mathematics for you, but if you'd like to have a look at the attached resource,

01:11.090 --> 01:21.680
it goes through a ten two and how that slightly differs from ten and why we can get away with finding

01:21.680 --> 01:27.410
that full angle when the angle is not actually inside of the right angle triangle that we constructed.

01:28.520 --> 01:28.880
All right.

01:28.880 --> 01:30.590
So let's get started with the script.

01:30.590 --> 01:35.720
We're going to create a new C-sharp script and we're going to call it A.I. Controller.

01:37.190 --> 01:41.150
And that A.I. controller is going to go on to our car.

01:41.300 --> 01:48.560
So once that finishes compiling, just drag and drop it over here onto the car and then just check that

01:48.560 --> 01:49.150
it's there.

01:49.160 --> 01:50.960
Right, let's open that up.

01:50.960 --> 01:57.290
So it needs to know about the drive script so they can actually call the go method for those.

01:57.350 --> 02:08.900
So in here we're going to first add a drive array of the drive scripts like that and then in the start

02:08.900 --> 02:09.950
we'll grab all of those.

02:09.950 --> 02:22.310
So drive scripts, DS is going to equal this get component in children drive.

02:23.300 --> 02:29.090
So that's just going to automatically pick all of those scripts up for you and make sure that you use

02:29.090 --> 02:35.540
the plural version of it, because we won't get components into the four of them or however many wheels

02:35.630 --> 02:39.320
that you've added to your particular vehicle.

02:39.350 --> 02:39.770
Okay.

02:39.770 --> 02:44.660
So besides all of those, we also want to get the circuit.

02:44.670 --> 02:48.620
So we're going to put public circuit circuit.

02:48.620 --> 02:53.870
And this will then point to the circuit that you've created for the car.

02:54.320 --> 03:06.350
We also need a public float for steering sensitivity and we'll set that to equal 0.01 F initially.

03:06.350 --> 03:12.350
Then we need a vector three for the target that we're currently going towards and we need an INT for

03:12.350 --> 03:13.970
our current waypoint.

03:13.970 --> 03:19.220
Now you've seen this before because we've actually looped around waypoints with our tanks that we've

03:19.220 --> 03:19.640
done.

03:19.640 --> 03:24.170
But in this particular case, it's a little bit different because we're steering towards the waypoints

03:24.170 --> 03:27.740
rather than just slipping the entire vehicle across.

03:27.920 --> 03:28.400
Okay.

03:28.400 --> 03:34.280
So what we want to do in the start, to begin with is set our target to the first waypoint so it'll

03:34.280 --> 03:46.430
equal circuit dot waypoints, current waypoint dot transform dot position like that.

03:46.430 --> 03:52.280
Okay, so just save that and we'll just switch back into unity and just set these things up.

03:52.280 --> 04:00.020
So with our car, we've got our circuit here, let's just drag and drop that into there so it can pick

04:00.020 --> 04:01.790
up those waypoints.

04:01.970 --> 04:05.840
And for a moment, let's go back into our drive script, okay?

04:05.900 --> 04:09.380
Because in here we want this code.

04:09.530 --> 04:16.880
Let's just grab this because this is not going to be used any longer inside of the drive.

04:16.880 --> 04:23.510
So we basically want the middle of this, but we're going to just then comment out this entire update

04:23.510 --> 04:25.730
because we don't want it running independently.

04:25.940 --> 04:31.790
Okay, so save that switch back to our controller and paste your code into here.

04:32.090 --> 04:38.420
Now this go needs to run on all of the drive scripts up here, so we're going to have a little for loop.

04:38.420 --> 04:47.630
So four int i equals zero i's less than d's dot length.

04:48.830 --> 04:50.120
You could also use a4h.

04:50.120 --> 04:59.270
I guess I'm just more familiar and go straight for a normal for loop when I'm coding and let's put this

04:59.270 --> 05:02.120
like this and then we need to get.

05:02.970 --> 05:11.550
Index of I dot go and run that go now you can see it's not liking it just here if we switch back to

05:11.550 --> 05:14.460
the drive script and you have a look at the go, it's not public.

05:14.460 --> 05:18.150
So we need to make that public so we can access it back in.

05:18.150 --> 05:20.490
I Controller So save that.

05:20.490 --> 05:27.300
Let's go back into here and you'll notice that it's no longer read so you can save that and that's going

05:27.300 --> 05:34.260
to run our car now the A and the SE we're no longer going to use.

05:34.260 --> 05:37.590
We actually want to program those values in.

05:37.590 --> 05:44.880
But with this in here now we can actually check if it's still working correctly, like the wheels and

05:44.880 --> 05:46.070
everything are going to.

05:46.110 --> 05:55.440
So we're going to now press play and just check that we've got all of that set up correctly.

05:56.070 --> 06:00.630
So the wheels are turning and we can still go forwards and we can turn.

06:00.900 --> 06:04.200
So those commands are getting through just nicely.

06:04.200 --> 06:04.650
Okay.

06:04.650 --> 06:08.640
So let's now continue looking at our air controller.

06:08.880 --> 06:14.700
So let's get rid of these values or in fact, let's just leave them in there because instead of using

06:14.700 --> 06:20.940
the get axis and the vertical and horizontal for those, we're going to program them based on the angles

06:20.940 --> 06:23.430
that we need to turn between our waypoints.

06:24.240 --> 06:26.310
Okay, let's get the rest of the code in.

06:26.310 --> 06:32.220
The first thing we're going to need at the top here is a rigid body for the rigid body that's attached

06:32.220 --> 06:36.930
to the car because we need to know how fast it's going, which is its speed.

06:36.930 --> 06:44.450
So in the start we'll go our be rigid body equals this dot get component rigid body.

06:44.460 --> 06:49.500
Okay, now in the update, let's come down and actually make the modifications that we need in here.

06:49.500 --> 06:54.330
Let's leave our acceleration and our steering in here as they are for now.

06:54.330 --> 07:00.990
But we'll go to work out the angle between the direction the car is facing and where the next waypoint

07:00.990 --> 07:01.380
is.

07:01.380 --> 07:06.480
And we have to do that in the local space, local to the actual car.

07:06.660 --> 07:13.710
So it means that basically the local value for the forward of the car is always going to be considered

07:13.710 --> 07:15.690
as it's zeroed angle.

07:15.690 --> 07:21.030
And then you've got to see how much it's got to turn from that position so we can get the local target

07:21.030 --> 07:33.270
from this dot transform dot inverse transform point, and that will get the target relative to the location

07:33.270 --> 07:33.780
of the car.

07:33.780 --> 07:38.520
So it doesn't matter where it is in the world or which way it's turned, it's going to be just local

07:38.520 --> 07:39.090
to it.

07:39.090 --> 07:49.890
So it considers the car basically at 000 and then we go float distance to target equals vector three

07:50.130 --> 07:53.280
dot distance between the target.

07:53.990 --> 07:56.600
And they transform.

07:58.110 --> 07:59.280
Dot position.

08:00.790 --> 08:02.980
And then we can calculate the angle.

08:02.980 --> 08:08.740
So float target angle equals math f dot.

08:09.890 --> 08:18.380
A ten to local target, ex local target.

08:19.750 --> 08:32.140
Dot Z multiplied by maf f dot rad to degrees because unity works when it does any angle calculations

08:32.140 --> 08:38.020
or angle functions works in radians and we need it in a well, we don't really need it in degrees.

08:38.020 --> 08:40.870
It just makes more sense to us when it's in degrees.

08:40.870 --> 08:49.090
Now, the acceleration, we will just put that as 0.5 F, we're just going to set it to a static acceleration

08:49.240 --> 08:50.980
and the steering.

08:50.980 --> 08:53.530
We're going to calculate that from our angle.

08:53.530 --> 08:56.260
So in here we'll go math f clamp.

08:56.530 --> 09:05.800
We're going to clamp the target angle multiplied by the steering sensitivity.

09:06.730 --> 09:11.890
We're going to clamp it between minus one and one, just like we did previously, because we basically

09:11.890 --> 09:15.460
want this to be a percentage of -1 to 1.

09:15.460 --> 09:20.980
Now, when we're working out the angle, it's going to be determined on which way we're actually facing

09:20.980 --> 09:22.630
or which way the car's facing.

09:22.630 --> 09:30.670
Because if it's facing, for example, up like this and you want to turn to this one, then you actually

09:30.670 --> 09:33.400
have to turn anti-clockwise.

09:33.400 --> 09:38.620
If the car is facing down and it has to go over here, then you actually in the world you have to face

09:38.620 --> 09:40.150
or rotate clockwise.

09:40.150 --> 09:46.960
So it does matter which way the car is facing and we can get that from the actual speed of the car,

09:46.960 --> 09:52.750
which is why we added our rigid body so we can go math f sine.

09:52.750 --> 09:54.820
We only even want to know the sign of the speed.

09:54.820 --> 10:02.980
So rigid body velocity magnitude which will be the speed, whether it's travelling forward or backwards.

10:03.250 --> 10:09.430
And once we've done that, we can then loop around and actually put everything through to our wheel

10:09.430 --> 10:10.210
drives.

10:10.780 --> 10:14.080
After that, we need to update our waypoints.

10:14.080 --> 10:23.710
So just a simple if statement here, if distance to target is less than two.

10:23.710 --> 10:31.930
So if we get within two of this particular waypoint, current waypoint will increase to the next one

10:31.930 --> 10:40.270
in the list if current waypoint is greater than or equal to the circuit waypoints length.

10:40.270 --> 10:43.180
So we've run out of waypoints.

10:43.180 --> 10:49.240
Then we need to just go back to the first one again because it's a circuit, so current waypoint will

10:49.240 --> 10:52.990
equal zero and then we can go target.

10:54.460 --> 11:08.560
Update that target equals circuit dot waypoints current waypoint dot transform position like that.

11:08.590 --> 11:10.860
Okay, so we're ready to try this out.

11:10.870 --> 11:15.780
And I've just noticed I have an extra bracket floating around the bottom of my class.

11:15.790 --> 11:17.020
Let me just get rid of that.

11:17.030 --> 11:17.430
Okay.

11:17.650 --> 11:19.780
Otherwise, the code is good to go.

11:19.780 --> 11:23.890
So let's save that switch back into unity.

11:23.890 --> 11:32.650
And now once you're in here, these waypoints are cubes, so you want to select them all and, and tick

11:32.650 --> 11:33.280
the box.

11:33.280 --> 11:37.390
Collider Okay, otherwise your car is going to run into them, which you don't want later on.

11:37.390 --> 11:39.700
If you don't even want to see the boxes, you can turn off the box.

11:39.700 --> 11:46.510
Collider I always use a cube so that I can just see a location in the world which helps for debugging

11:46.510 --> 11:47.560
purposes, right?

11:47.830 --> 11:49.990
Let's press play and have a look what happens.

11:49.990 --> 11:52.180
So all the car is turning.

11:52.720 --> 11:54.280
Now, where is it going to go to?

11:54.280 --> 11:55.300
This is Waypoint one.

11:55.300 --> 11:59.350
If I select it, you can actually see that it's just there now.

11:59.350 --> 12:07.900
It's trying to get to 8.1 and it's going round waypoint one and will it go to a point two?

12:07.900 --> 12:09.070
Now it's circling around.

12:09.070 --> 12:11.860
Now this is a common issue that you have with waypoints.

12:11.860 --> 12:17.170
And can you remember why we've already covered this in other waypoint tutorials?

12:17.170 --> 12:18.730
So this is the challenge.

12:18.730 --> 12:27.370
So if you can just fix that before we come back and get the car to go around each of these waypoints

12:27.370 --> 12:27.910
pause video.

12:27.910 --> 12:30.610
Now, when I come back, I'll continue going through that with you.

12:31.540 --> 12:31.990
Okay.

12:31.990 --> 12:32.590
Welcome back.

12:32.590 --> 12:33.580
How did you go?

12:33.580 --> 12:41.410
Well, the reason that a NPC is going to circle a waypoint is because it can never get close enough

12:41.410 --> 12:45.340
to the distance value here to be considered close to it.

12:45.340 --> 12:49.900
So I'm actually going to change this to for like that instead.

12:49.900 --> 12:56.770
So we can definitely get within four of this waypoint, save that, let's go back into unity.

12:57.010 --> 13:02.320
Of course, you can also increase the steering sensitivity and the maximum turn on all those sorts of

13:02.320 --> 13:04.090
things, which would also influence on that.

13:04.090 --> 13:08.230
But this is a car, so you don't really want it to turn too sharply.

13:08.350 --> 13:08.800
Okay.

13:08.800 --> 13:14.140
So now that it's considered that it has reached Waypoint one, which is this one here, the second waypoint

13:14.140 --> 13:15.820
is this one here.

13:16.390 --> 13:19.150
So off it goes down to there.

13:19.180 --> 13:25.570
Then once it has, it's going to turn towards Waypoint three and go through that one.

13:26.020 --> 13:31.300
And then eventually off it goes up to the fourth one.

13:31.300 --> 13:36.430
And so now it's actually nicely going around our circuit.

13:37.210 --> 13:37.810
Okay.

13:37.810 --> 13:41.110
So that's the basic waypoint system and steering in place.

13:41.110 --> 13:48.010
When we come back in the next video, we're actually going to work on enhancing this code for our AI

13:48.040 --> 13:48.910
controller.
