WEBVTT

00:07.070 --> 00:13.910
In this lecture, we're going to start working on a braking system so that the vehicles can slow down

00:13.910 --> 00:14.720
into corners.

00:14.720 --> 00:20.060
Now, you'll see here that I've added a race track model into my project.

00:20.060 --> 00:27.290
Now you can get a hold of this race track model if you get what's attached to this lecture, the package,

00:27.290 --> 00:32.030
and bring it into your assets, you'll get a race track folder that comes in.

00:32.030 --> 00:35.060
There'll be some materials and other things in there.

00:35.090 --> 00:40.340
The thing that you want to drag and drop into your scene is this race track here, which is the prefab

00:40.340 --> 00:42.380
don't drag in the model.

00:42.380 --> 00:44.570
The model's got the little triangle next to it.

00:44.570 --> 00:51.440
You want this actual prefab now it's going to be quite large and it also goes below the ground.

00:51.440 --> 00:56.960
So there's my ground plane and you'll see that if I have a look under the ground, it's quite off the

00:56.960 --> 00:59.240
ground where I just want to stay on the ground for now.

00:59.240 --> 01:02.330
So I've got it lowered down through my ground plane.

01:02.480 --> 01:07.580
The other thing you'll need to do is possibly add more waypoints because I'm sure that you didn't have

01:07.580 --> 01:09.830
as many waypoints as this.

01:09.830 --> 01:13.040
Now you can see I've put my car on to the track.

01:13.580 --> 01:19.580
I've put the first waypoint, which is this one here in front of the car with the car sort of going

01:19.580 --> 01:22.160
to be able to get to it quite easily.

01:22.160 --> 01:26.660
And then in the corners you're going to have to create more waypoints.

01:26.660 --> 01:32.210
If I click on my circuit, which shows me the white lines around it, you can see that you're going

01:32.210 --> 01:35.690
to need to add more waypoints around these corners.

01:35.840 --> 01:42.050
When you add the waypoints, just make sure that they're sitting slightly above the ground, or at least

01:42.050 --> 01:48.800
their center point is so that you can see that white line that gets drawn in the circuit quite easily

01:48.800 --> 01:50.870
just like that.

01:50.930 --> 01:51.380
Okay.

01:51.380 --> 01:53.540
So go ahead and do that.

01:53.540 --> 01:59.810
Once you've added all these waypoints, don't forget to put them in in order into the waypoints in the

01:59.810 --> 02:02.420
inspector for your circuit script.

02:02.420 --> 02:07.400
Otherwise it's not going to show up and your car won't be able to find them.

02:07.790 --> 02:16.220
Right now, let's go to our car and add in a brake light and I'm just going to lift my car up because

02:16.220 --> 02:20.060
it actually has to sit on the road, not halfway under it.

02:20.600 --> 02:24.260
The brake light will use for some feedback for ourselves.

02:24.260 --> 02:28.520
So we know when our car is actually braking as it's going around the track.

02:28.790 --> 02:31.700
So select the car, right click.

02:31.970 --> 02:35.480
We're going to go down to light and we want spotlight.

02:35.480 --> 02:40.310
Now, that spotlight isn't necessarily going to be where you want it to be, which is down there.

02:40.310 --> 02:43.550
I want it showing up on the license plate.

02:43.550 --> 02:49.040
So let's just go back and move it around until you can see it on the license plate.

02:49.040 --> 02:54.890
Make sure that you've got lighting in your scene turned on, otherwise you won't be able to see this.

02:55.220 --> 03:01.070
Now, once you've got it sort of way like the illumination come over to the light setting can actually

03:01.070 --> 03:03.110
increase the intensity of that.

03:03.110 --> 03:08.930
So it's very obvious that the brakes are on and if you like a red brake light, then you can make it

03:08.930 --> 03:10.310
red as well.

03:10.340 --> 03:16.340
The range you could turn down because you don't need it to try and light any further than there.

03:16.340 --> 03:19.520
So just check what you're going to get as you bring it in.

03:19.520 --> 03:25.760
And I'm going to turn that intensity way up so that we can really see it on the car.

03:25.760 --> 03:29.360
Now let's change the name of that to brake light.

03:29.630 --> 03:35.960
And we're also going to turn it off in the inspector because initially the car isn't braking, so we

03:35.960 --> 03:37.430
don't want it on.

03:37.850 --> 03:38.270
Right?

03:38.270 --> 03:47.690
So let's go into our script and we want to go into the eye controller and of course the drive script,

03:47.960 --> 03:53.750
the air controller is going to run, the drive scripts go down here.

03:53.780 --> 03:55.100
Okay, now go.

03:55.100 --> 03:59.810
If we go into the drive script, which is doing all the work for us, we want to add an extra variable

03:59.810 --> 04:00.650
at the end here.

04:01.040 --> 04:04.790
We'll call it float and it'll be called brake.

04:05.240 --> 04:12.320
So just save that there and we'll go back into our air controller and down here we're going to send

04:12.320 --> 04:13.940
through a brake value.

04:13.940 --> 04:22.790
So float B equals zero initially and then we'll pass that through here to go.

04:22.820 --> 04:23.660
Okay, great.

04:23.660 --> 04:25.160
So we're going to save that.

04:25.160 --> 04:33.950
Now the AI controller is going to turn our brake light on and off depending on the value of B.

04:33.950 --> 04:39.830
So let's go up the top here and pass through our controller.

04:39.830 --> 04:46.100
So we want a public game object, not controller, I mean a game object of our brake light.

04:46.340 --> 04:49.940
So call it brake light like that.

04:50.060 --> 04:58.580
And then once that's passed through, we want to test it under here after it's been set.

04:58.580 --> 05:02.660
Or it could go before actually, it doesn't really matter where you put it as long as it's after brake

05:02.660 --> 05:06.650
has been calculated or B and we can say if b.

05:06.730 --> 05:08.740
Is greater than zero.

05:09.310 --> 05:14.050
Then we're going to set the brake light.

05:15.520 --> 05:19.330
Dot set active to true.

05:22.580 --> 05:23.390
Else.

05:25.380 --> 05:27.810
We will set it to false.

05:33.200 --> 05:33.800
Okay.

05:33.800 --> 05:35.630
So we can save those.

05:35.630 --> 05:38.000
There's nothing to actually look at at the moment.

05:38.000 --> 05:45.320
Let's go back into our car and we'll just hook up the car's air controller with the brake light.

05:45.330 --> 05:52.340
So just grab your brake light and drop it in in the inspector so that we've got access to turn that

05:52.340 --> 05:53.450
on and off.

05:53.690 --> 05:54.320
Right.

05:54.320 --> 06:00.320
How are we going to calculate the amount of breakage that we need?

06:02.770 --> 06:08.230
Well, first we'll just get to the dynamics of breaking and actually put it into our script.

06:08.230 --> 06:11.770
So we want to go into the drive script at the very top.

06:11.770 --> 06:16.150
We're going to add in a in the drive script.

06:16.150 --> 06:26.980
That should be we're going to add in a max braking torque that we can set so public float max break

06:27.610 --> 06:28.510
torque.

06:28.840 --> 06:33.760
And we're going to set that to 500 to begin with.

06:33.850 --> 06:34.210
Okay.

06:34.210 --> 06:40.990
Now, down in our go where this brake value is coming through, we want to use it with the brake just

06:40.990 --> 06:44.410
like we've used the max steering angle with the steer.

06:44.410 --> 06:49.570
So down in here, I'm going to put an else because we're going to have just the back tires that are

06:49.570 --> 06:50.770
the braking ones.

06:51.340 --> 06:56.650
And in here we'll go brake equals mat clamp.

06:58.330 --> 07:01.930
Break that's being passed through -1 to 1.

07:01.930 --> 07:08.650
And then we're going to multiply that up by our max break torque that we're applying to it.

07:08.950 --> 07:09.520
Okay.

07:09.520 --> 07:15.130
So how do we actually then apply that talk to these back wheels?

07:15.310 --> 07:17.590
It's very similar to the steer angle case.

07:17.590 --> 07:26.740
So it's going to be W.C. Dot break torque, which will equal our brake value like that there.

07:27.040 --> 07:27.670
Okay.

07:27.670 --> 07:30.100
So we're going to save that now.

07:30.100 --> 07:36.550
We're going to go back into our controller because it's what is going to control down in our update

07:36.550 --> 07:43.660
the value of B, which is our brake to determine if we actually need to brake or not.

07:43.690 --> 07:51.880
Now what you'll see I've done is I've increased the acceleration as well to 50 and in the drive I should

07:51.880 --> 07:58.780
tell you that I also changed my max steer angle to 60 just to give the car a little bit of help and

07:58.780 --> 07:59.500
getting around.

07:59.500 --> 08:04.450
And so that it drives a bit faster because it's not driving fast and it doesn't doesn't really need

08:04.450 --> 08:06.670
to brake as much.

08:06.790 --> 08:07.330
Okay.

08:07.330 --> 08:15.430
So with all of those values in with zero braking, let's just have a look how these higher values affect

08:15.430 --> 08:18.400
the car's ability to stick to the circuit.

08:18.610 --> 08:20.590
Okay, so make sure everything is saved.

08:20.590 --> 08:23.260
Then let's switch back to Unity and give it a go.

08:24.280 --> 08:28.300
Let's before we play, touch the camera to our car.

08:28.300 --> 08:34.240
So let's just line up the scene where we would like the sort of the camera view to be.

08:34.240 --> 08:40.900
Then what you want to do is select the main camera that's in the scene minus down the bottom here and

08:40.900 --> 08:43.840
we're going to go game object align with view.

08:43.840 --> 08:49.330
So they were looking at the car and then we're going to get the camera and drag and drop it on to the

08:49.330 --> 08:49.690
car.

08:49.690 --> 08:53.170
So it's going to go everywhere the car goes.

08:53.410 --> 08:53.920
Right.

08:53.920 --> 08:58.600
Let's press play and have a look at how our driving goes.

08:58.780 --> 09:02.080
The brake light shouldn't come on because we're setting that to zero.

09:02.440 --> 09:13.360
Okay, so let's just zoom out in the scene so we can see how our driving is going to go and we can zoom

09:13.360 --> 09:15.310
in down in this corner to have a look.

09:15.310 --> 09:17.320
Now it's coming up to this corner.

09:17.680 --> 09:20.560
It doesn't currently have the ability to slow down.

09:20.560 --> 09:29.560
So what's going to happen is that it's trying to get around, but it hit the wall because it didn't

09:29.560 --> 09:31.510
quite reach that waypoint.

09:31.510 --> 09:38.860
And now it's kind of stuck trying to get to that waypoint by going round and round, and maybe eventually

09:38.860 --> 09:42.640
it might get to that particular waypoint and then continue on.

09:43.690 --> 09:46.030
But let's not rely on it for now.

09:46.060 --> 09:46.450
Okay.

09:46.450 --> 09:54.820
So essentially what we want is the car to slow down into the corner so that can actually make the turn.

09:55.960 --> 10:00.460
So back in the AI controller code, we'll start with something simple.

10:00.460 --> 10:02.650
This is in our update method.

10:02.650 --> 10:04.810
We're going to check for our distance to target.

10:04.810 --> 10:07.780
If we're getting close to that target, then we want to put on the brakes.

10:07.780 --> 10:14.740
So we're going to put if distance to target is, let's say, less than ten.

10:14.980 --> 10:21.820
And what we're going to do is set the brake value to equal 0.5.

10:21.820 --> 10:31.210
Now, remember, these values are basically between zero and one that we're sending through to go because

10:31.210 --> 10:32.620
it ends up normalizing it.

10:32.620 --> 10:37.420
If we go back to the drive script and have a look at what it does, it actually is running a clamp on

10:37.420 --> 10:39.160
the values that are coming through.

10:39.160 --> 10:43.990
So the fact that I've even set this acceleration to FIFO up here is not going to make much sense.

10:44.170 --> 10:50.500
So I'm going to go back to put it at one, which is the full amount of acceleration.

10:50.500 --> 10:57.190
And then in order to actually influence these types of things here so that they're greater than one,

10:57.190 --> 11:05.020
if I save that flip back into our drive script, we need to change the maximum values up here.

11:05.020 --> 11:10.090
So our torque value, our max steer angle and our max brake torque.

11:10.240 --> 11:10.750
Okay.

11:10.750 --> 11:21.220
So with this bit of code in here, let's just press play and see how we go for a first try.

11:21.460 --> 11:23.560
Okay, so off we go.

11:24.430 --> 11:31.660
And he sees braking coming into there because he got less than ten away from it.

11:31.660 --> 11:36.970
Now, once he got within the right range and say, I've been there, let's go to the next way point.

11:36.970 --> 11:42.970
He still had enough acceleration to keep going through that all momentum, I should say, to keep going

11:43.030 --> 11:43.200
through.

11:43.210 --> 11:47.950
So you should break into this one and then.

11:48.430 --> 11:53.990
Yeah, not not too bad actually it's going to break and I'm not sure whether he'll make it all the way

11:53.990 --> 11:57.250
around, but he might just have enough accelerations to still.

11:57.690 --> 11:58.230
Get around?

11:58.260 --> 11:58.580
Yep.

11:58.620 --> 11:59.610
Okay, great.

11:59.730 --> 12:06.210
So if your car didn't make it around, then you've got the option of playing and tweaking with the values

12:06.210 --> 12:07.410
that are on your wheel.

12:07.410 --> 12:07.920
Colliders.

12:07.920 --> 12:16.650
If you select them all here, you can access the torque, the max steer angle and the max brake torque

12:16.650 --> 12:17.610
within here.

12:17.610 --> 12:23.820
And in fact, this just to make it more consistent, if we just jump back into our air controller code

12:23.940 --> 12:29.580
and go up to the top note Sorry, it's in the drive code up here.

12:30.060 --> 12:31.050
We've got this value.

12:31.050 --> 12:37.860
Let's just I'm just going to take it out of there and put it in here because essentially this is our

12:37.860 --> 12:39.630
max torque.

12:40.170 --> 12:45.810
So if we save that and make sure that we update it down here, Max.

12:47.470 --> 12:53.140
Talk, then back in our script.

12:54.560 --> 12:57.620
Then back in the inspector, we can select those three things.

12:57.620 --> 13:04.160
And again, you've got those values that are now all sort of together that you can modify down in here.

13:04.370 --> 13:04.810
Okay.

13:04.820 --> 13:10.610
When we come back in the next lecture, we'll keep improving on this driving.
