In this lecture, we're going to start working on a braking system so that the vehicles can slow down
into corners.
Now, you'll see here that I've added a race track model into my project.
Now you can get a hold of this race track model if you get what's attached to this lecture, the package,
and bring it into your assets, you'll get a race track folder that comes in.
There'll be some materials and other things in there.
The thing that you want to drag and drop into your scene is this race track here, which is the prefab
don't drag in the model.
The model's got the little triangle next to it.
You want this actual prefab now it's going to be quite large and it also goes below the ground.
So there's my ground plane and you'll see that if I have a look under the ground, it's quite off the
ground where I just want to stay on the ground for now.
So I've got it lowered down through my ground plane.
The other thing you'll need to do is possibly add more waypoints because I'm sure that you didn't have
as many waypoints as this.
Now you can see I've put my car on to the track.
I've put the first waypoint, which is this one here in front of the car with the car sort of going
to be able to get to it quite easily.
And then in the corners you're going to have to create more waypoints.
If I click on my circuit, which shows me the white lines around it, you can see that you're going
to need to add more waypoints around these corners.
When you add the waypoints, just make sure that they're sitting slightly above the ground, or at least
their center point is so that you can see that white line that gets drawn in the circuit quite easily
just like that.
Okay.
So go ahead and do that.
Once you've added all these waypoints, don't forget to put them in in order into the waypoints in the
inspector for your circuit script.
Otherwise it's not going to show up and your car won't be able to find them.
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
it actually has to sit on the road, not halfway under it.
The brake light will use for some feedback for ourselves.
So we know when our car is actually braking as it's going around the track.
So select the car, right click.
We're going to go down to light and we want spotlight.
Now, that spotlight isn't necessarily going to be where you want it to be, which is down there.
I want it showing up on the license plate.
So let's just go back and move it around until you can see it on the license plate.
Make sure that you've got lighting in your scene turned on, otherwise you won't be able to see this.
Now, once you've got it sort of way like the illumination come over to the light setting can actually
increase the intensity of that.
So it's very obvious that the brakes are on and if you like a red brake light, then you can make it
red as well.
The range you could turn down because you don't need it to try and light any further than there.
So just check what you're going to get as you bring it in.
And I'm going to turn that intensity way up so that we can really see it on the car.
Now let's change the name of that to brake light.
And we're also going to turn it off in the inspector because initially the car isn't braking, so we
don't want it on.
Right?
So let's go into our script and we want to go into the eye controller and of course the drive script,
the air controller is going to run, the drive scripts go down here.
Okay, now go.
If we go into the drive script, which is doing all the work for us, we want to add an extra variable
at the end here.
We'll call it float and it'll be called brake.
So just save that there and we'll go back into our air controller and down here we're going to send
through a brake value.
So float B equals zero initially and then we'll pass that through here to go.
Okay, great.
So we're going to save that.
Now the AI controller is going to turn our brake light on and off depending on the value of B.
So let's go up the top here and pass through our controller.
So we want a public game object, not controller, I mean a game object of our brake light.
So call it brake light like that.
And then once that's passed through, we want to test it under here after it's been set.
Or it could go before actually, it doesn't really matter where you put it as long as it's after brake
has been calculated or B and we can say if b.
Is greater than zero.
Then we're going to set the brake light.
Dot set active to true.
Else.
We will set it to false.
Okay.
So we can save those.
There's nothing to actually look at at the moment.
Let's go back into our car and we'll just hook up the car's air controller with the brake light.
So just grab your brake light and drop it in in the inspector so that we've got access to turn that
on and off.
Right.
How are we going to calculate the amount of breakage that we need?
Well, first we'll just get to the dynamics of breaking and actually put it into our script.
So we want to go into the drive script at the very top.
We're going to add in a in the drive script.
That should be we're going to add in a max braking torque that we can set so public float max break
torque.
And we're going to set that to 500 to begin with.
Okay.
Now, down in our go where this brake value is coming through, we want to use it with the brake just
like we've used the max steering angle with the steer.
So down in here, I'm going to put an else because we're going to have just the back tires that are
the braking ones.
And in here we'll go brake equals mat clamp.
Break that's being passed through -1 to 1.
And then we're going to multiply that up by our max break torque that we're applying to it.
Okay.
So how do we actually then apply that talk to these back wheels?
It's very similar to the steer angle case.
So it's going to be W.C. Dot break torque, which will equal our brake value like that there.
Okay.
So we're going to save that now.
We're going to go back into our controller because it's what is going to control down in our update
the value of B, which is our brake to determine if we actually need to brake or not.
Now what you'll see I've done is I've increased the acceleration as well to 50 and in the drive I should
tell you that I also changed my max steer angle to 60 just to give the car a little bit of help and
getting around.
And so that it drives a bit faster because it's not driving fast and it doesn't doesn't really need
to brake as much.
Okay.
So with all of those values in with zero braking, let's just have a look how these higher values affect
the car's ability to stick to the circuit.
Okay, so make sure everything is saved.
Then let's switch back to Unity and give it a go.
Let's before we play, touch the camera to our car.
So let's just line up the scene where we would like the sort of the camera view to be.
Then what you want to do is select the main camera that's in the scene minus down the bottom here and
we're going to go game object align with view.
So they were looking at the car and then we're going to get the camera and drag and drop it on to the
car.
So it's going to go everywhere the car goes.
Right.
Let's press play and have a look at how our driving goes.
The brake light shouldn't come on because we're setting that to zero.
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
in down in this corner to have a look.
Now it's coming up to this corner.
It doesn't currently have the ability to slow down.
So what's going to happen is that it's trying to get around, but it hit the wall because it didn't
quite reach that waypoint.
And now it's kind of stuck trying to get to that waypoint by going round and round, and maybe eventually
it might get to that particular waypoint and then continue on.
But let's not rely on it for now.
Okay.
So essentially what we want is the car to slow down into the corner so that can actually make the turn.
So back in the AI controller code, we'll start with something simple.
This is in our update method.
We're going to check for our distance to target.
If we're getting close to that target, then we want to put on the brakes.
So we're going to put if distance to target is, let's say, less than ten.
And what we're going to do is set the brake value to equal 0.5.
Now, remember, these values are basically between zero and one that we're sending through to go because
it ends up normalizing it.
If we go back to the drive script and have a look at what it does, it actually is running a clamp on
the values that are coming through.
So the fact that I've even set this acceleration to FIFO up here is not going to make much sense.
So I'm going to go back to put it at one, which is the full amount of acceleration.
And then in order to actually influence these types of things here so that they're greater than one,
if I save that flip back into our drive script, we need to change the maximum values up here.
So our torque value, our max steer angle and our max brake torque.
Okay.
So with this bit of code in here, let's just press play and see how we go for a first try.
Okay, so off we go.
And he sees braking coming into there because he got less than ten away from it.
Now, once he got within the right range and say, I've been there, let's go to the next way point.
He still had enough acceleration to keep going through that all momentum, I should say, to keep going
through.
So you should break into this one and then.
Yeah, not not too bad actually it's going to break and I'm not sure whether he'll make it all the way
around, but he might just have enough accelerations to still.
Get around?
Yep.
Okay, great.
So if your car didn't make it around, then you've got the option of playing and tweaking with the values
that are on your wheel.
Colliders.
If you select them all here, you can access the torque, the max steer angle and the max brake torque
within here.
And in fact, this just to make it more consistent, if we just jump back into our air controller code
and go up to the top note Sorry, it's in the drive code up here.
We've got this value.
Let's just I'm just going to take it out of there and put it in here because essentially this is our
max torque.
So if we save that and make sure that we update it down here, Max.
Talk, then back in our script.
Then back in the inspector, we can select those three things.
And again, you've got those values that are now all sort of together that you can modify down in here.
Okay.
When we come back in the next lecture, we'll keep improving on this driving.