WEBVTT

00:07.230 --> 00:16.710
In the previous lecture, we added in some code to calculate the acceleration and the braking factors

00:16.710 --> 00:19.820
based on the corner that's about to be turned.

00:19.830 --> 00:21.390
Now, it's not good enough.

00:21.390 --> 00:28.770
We found, if we're trying to go towards a particular waypoint and not looking ahead to the next waypoint

00:28.770 --> 00:35.670
because it's really whatever's coming next, that's going to be the turning angle that we need to take.

00:35.670 --> 00:42.150
So in that case, we can actually implement a progress tracker, which is kind of one waypoint ahead

00:42.150 --> 00:46.230
of the car, and the car can follow the progress tracker instead.

00:46.230 --> 00:53.490
So let's come up to the top of our AI controller code and we're going to add in a game object called

00:53.490 --> 00:54.270
Tracker.

00:56.070 --> 01:06.270
And we're going to have an inte current tracker waypoint equals zero and we want to float, look ahead

01:07.020 --> 01:09.480
and we'll have that set initially to 40.

01:09.480 --> 01:16.340
That's a that's how far ahead you want the tracker to be to the vehicle itself.

01:16.350 --> 01:16.600
Okay.

01:16.650 --> 01:18.810
So in start, we're going to create this tracker.

01:18.810 --> 01:29.490
So the tracker is going to equal game object dot create primitive and I'm going to make a capsule primitive

01:29.490 --> 01:31.110
dot capsule.

01:32.610 --> 01:39.900
And this is just so we can see it and we're going to destroy immediate the collider on that because

01:39.900 --> 01:44.280
we don't want to have the car tripping over it or getting stuck anywhere.

01:44.460 --> 01:50.400
So we're going to get component of the collider and remove that.

01:51.750 --> 01:56.850
Now I'm going to put this line in, but I'm going to comment it out because you can remove it a little

01:56.850 --> 01:57.360
bit later.

01:57.360 --> 02:03.540
And basically what it is, is the mesh renderer for the capsule so you can get rid of it when you don't

02:03.540 --> 02:05.280
need to see it anymore.

02:05.280 --> 02:21.270
So tracker get component and that will be a mesh renderer and we're going to put dot enabled.

02:23.750 --> 02:25.220
Equals false.

02:27.450 --> 02:33.030
Then we'll have tracker dot transform.

02:33.780 --> 02:39.600
Dot position equals this dot transform dot position.

02:39.600 --> 02:50.250
So we're putting it where the car is and then we going to have tracker dot transform dot rotation equals

02:50.250 --> 02:53.070
this dot transform dot rotation.

02:53.070 --> 02:57.030
So it's facing in the same direction as the vehicle.

02:57.300 --> 02:58.170
Okay, great.

02:58.170 --> 03:00.410
So that's setting up the tracker.

03:00.420 --> 03:08.490
Now let's create a progress tracker method, I guess, void progress tracker.

03:09.000 --> 03:12.000
And this is going to basically get our progress.

03:12.060 --> 03:14.430
I got to go around all of the waypoints.

03:14.430 --> 03:17.250
So we've already been doing this with the car.

03:17.370 --> 03:24.540
We want to debug dot draw line and this line is so that we can see which capsule belongs to which car

03:24.540 --> 03:25.920
and also the angle between them.

03:25.920 --> 03:35.400
So this dot transform dot position and tracker dot transform.

03:36.060 --> 03:41.070
Spell that right tracker dot transform dot position.

03:43.360 --> 03:58.490
Alright then if vector three dot distance this dot transform dot position tracker dot transform dot

03:58.600 --> 04:04.360
position is greater then look ahead.

04:06.990 --> 04:10.320
Then we're going to stop our tracker from moving.

04:10.590 --> 04:16.290
So the look ahead is having our tracker on like a piece of elastic and it can only get that far away

04:16.290 --> 04:18.840
before it stops and waits for the car to catch up.

04:18.850 --> 04:31.800
Okay, so a tracker transform look at and we're going to look at the circuit dot waypoints in brackets.

04:31.800 --> 04:38.460
The current tracker waypoint dot transformed position.

04:39.650 --> 04:45.410
The Troika has the luxury of using a lockout, which is going to snap it around and look straight at

04:45.410 --> 04:49.870
where it needs to go, which means it's going to end up getting quite close to where it is.

04:49.880 --> 04:56.180
So we don't to worry about slurps and all those sorts of things making the performance inaccurate.

04:56.180 --> 05:04.100
So Tracker Transform, Translate and zero zero.

05:04.100 --> 05:06.410
This is how fast you want your tracker to go.

05:06.410 --> 05:14.900
Now if you find that the tracker is like really quite close to the car and stuck with the car and never

05:14.900 --> 05:19.520
gets a far enough away, then of course you could increase this value.

05:19.520 --> 05:23.230
I've found with the current settings it seems to be just fine.

05:23.240 --> 05:29.870
Okay, so we've got that and that's pretty much all we need to do for moving the tracker.

05:29.870 --> 05:45.440
And then if vector three dot distance of the tracker dot transform position and the circuit dot waypoints

05:46.490 --> 05:54.770
current tracker waypoint dot transform dot position is less than one means.

05:54.770 --> 05:59.300
We've hit that waypoint and so we're going to update it.

05:59.750 --> 06:06.350
Let me just see if I can put this line down here so that you can see a little bit clearer what we've

06:06.350 --> 06:06.830
done.

06:06.830 --> 06:07.130
Okay.

06:07.130 --> 06:14.810
So after this inside of here, we'll have current tracker waypoint plus plus.

06:14.930 --> 06:21.350
Then if current tracker waypoint is greater than or equal to the number of them.

06:21.350 --> 06:25.060
Circuit dot waypoints.

06:25.070 --> 06:26.210
Dot length.

06:27.760 --> 06:29.530
Then current.

06:31.100 --> 06:35.870
Check point equals zero.

06:36.170 --> 06:37.240
Like that.

06:37.250 --> 06:40.010
Let's get rid of that space in there.

06:40.250 --> 06:45.320
And then we've got that bracket there to finish off our progress tracker.

06:45.620 --> 06:46.670
Okay, great.

06:46.820 --> 06:50.060
Now this has to actually run and stay ahead of our car.

06:50.060 --> 06:54.440
So we're going to update its position inside of the update.

06:54.440 --> 07:02.030
So the first thing in here we will do is put progress tracker and just run that.

07:02.390 --> 07:05.510
And then after that, we're going to set the target for the car.

07:05.510 --> 07:11.990
So the target, rather than being a waypoint, is going to be the progress tracker.

07:12.260 --> 07:17.720
So tracker dot transform, dot position.

07:18.140 --> 07:18.650
All right.

07:18.650 --> 07:26.240
So the target now gets used to work out the distances and the angles, which is all good and everything

07:26.240 --> 07:33.260
else is good until we get down to the bottom where we're updating our waypoints and our targets for

07:33.260 --> 07:34.640
our AI controller.

07:34.640 --> 07:37.150
And let's just comment this out because we don't need it any more.

07:37.160 --> 07:40.760
We're going to follow that waypoint tracker instead.

07:40.970 --> 07:47.060
Okay, so let's save this and we'll go back into unity.

07:47.660 --> 07:53.540
And what I'm going to do is just go through some of the settings I've been playing around with on my

07:53.540 --> 07:57.620
wheels, which might also influence how it behaves and how yours behave.

07:57.620 --> 07:59.660
So again, select all your wheel colliders.

07:59.840 --> 08:09.410
My maximum torque is at 300, my max steer angles at 90, my max break talk is at 5000, and in my other

08:09.410 --> 08:16.700
settings I have not changed the friction or I did actually play around a bit with the stiffness of it

08:16.700 --> 08:18.980
and I found that I'm just going to stay with one.

08:18.980 --> 08:23.330
The other values I haven't changed and they should be like you already had.

08:23.600 --> 08:29.660
Okay, so now what we're going to see when we press play is a capsule pop up that the car is going to

08:29.660 --> 08:30.190
follow.

08:30.200 --> 08:33.140
So let's see how it performs.

08:33.140 --> 08:33.440
All right.

08:33.440 --> 08:34.430
So off it goes.

08:34.430 --> 08:39.800
Now, notice it gets so far and then it just, like, waits until the car catches up.

08:39.920 --> 08:47.270
And then once we get up here and it starts to go over to this waypoint, you'll see that the car actually

08:47.270 --> 08:51.260
will break a lot earlier into that corner.

08:54.430 --> 08:56.020
And off it goes.

08:56.020 --> 09:00.250
And it's not too bad what I've achieved with this so far.

09:00.250 --> 09:06.010
And that was mostly fiddling around with these values, which is what you will have to do.

09:06.070 --> 09:13.540
Okay, so we're coming up to this corner here and the waypoint tracker is going to be over there before

09:13.540 --> 09:13.960
it actually.

09:13.960 --> 09:20.680
And now it's actually flipped and then it's going to land upside down because that's it's flip and off

09:20.680 --> 09:21.550
it goes again.

09:21.550 --> 09:24.820
So not perfect, but getting better.

09:24.820 --> 09:33.610
And you can, of course, play with the distance between your waypoint tracker and the vehicle.

09:33.850 --> 09:34.290
Okay.

09:34.300 --> 09:41.170
Now, one thing that is a bit of an issue is not in this corner, but you would have seen up the other

09:41.170 --> 09:49.090
end is that the car flips over because it doesn't seem to have enough sort of give in the suspension

09:49.450 --> 09:50.950
at that time on one side.

09:50.950 --> 09:52.210
And therefore, it just flips.

09:52.210 --> 09:58.300
And we're coming up to flip the car again and notice that when it does do that, it just it just dips

09:58.300 --> 09:59.350
straight over, okay.

09:59.380 --> 10:02.530
A normal car is going to sort of slide on one side.

10:02.530 --> 10:07.660
The wheels might lift up on here and these will actually take on a bit more of the pressure.

10:07.660 --> 10:14.410
So we'll come back in the next lecture and look at how to implement anti rolling code.
