In the previous video we added in breaking.
Now we're going to actually look at the forces that are on a car and how they affect the car's performance
and how it can actually play around with those in the wheel.
Collider.
So we've got this car and it's breaking in.
In the previous example, it wasn't actually too bad the way it got around the track.
If we increased the max torque here to say, Oh, fast 2000 and then we pressed play, and if you do
that, don't forget, you have to select all of the wheels on that particular car to do it.
Now, what happens to this car as we approach the corner?
Our braking distance is way too short for the speed that we're going and we end up flipping the car
over.
But the other thing is that now the car's wheels will actually slide.
So you can see he actually slid out of this corner and again rolled over and we'll fix that a little
bit later.
But you've got this slippage that goes on as well, and they're actually controlled by these different
friction values that we've got in the wheel.
Collider So let's pause for a moment and just talk about those particular friction forces.
The mechanics of friction on real rubber wheels is such that as the force is increasing, the slip stays
relatively low.
However, at some point called the extreme, the slip increases and thus the force is reduced.
It continues to reduce to the point that the force remains constant and the slip just increases.
Now remember that it's the torque being applied that increases the force on the tire.
So this graph is saying that a torque can be applied until a maximum force is hit, at which the car
will start to slip much more than it did before.
If you applied a great deal of initial torque such that the force on the tire immediately exceeds the
extreme, then you'll get an awful lot of slip.
Again, to understand this, go back to what you know of the real world.
If you start to move a car forward slowly, the wheels aren't going to slip.
However, if you floor the accelerator and immediately apply a great deal of force to the wheels, then
they'll start to spin out.
Now these values here can be a little tricky to set, and we're not going to really go into the dynamics
of these, but do feel free if you want to create sort of different cars with different friction values
that you can play with these and possibly multiply the number of cars you've got of the same set up
and then change these values.
But what I will show you is the friction, stiffness, and that's the one that's going to give you the
most sort of instant change in slippage.
If we set the stiffness here to zero for the forward friction and I'm looking at my car in this scene
sideways, what do you think is actually going to happen if you've got no friction on the wheels, which
means it's not going to grip whatsoever.
If we press play in this case, we will find that.
It's trying to go somewhere, but it can't.
The wheels are just going around and constantly slipping, basically on the spot.
It wants to go so fast.
So if we then turn this up to one, then we've got a higher stiffness.
But you can actually keep increasing up towards infinity.
Now, if you turn these stiffness values up a lot so that there's a lot of grip, then you've got more
chance of rolling the car over when you go around the corner, because it's not like slipping around
the corner and you might want it to sort of slip around the corner rather than roll.
So it's going to be a matter of finding that sweet spot in the stiffness values for the exact type of
effect that you're after.
Now, this current car, the way we've got it, is actually slipping a little bit around the corners.
But we've got this maximum torque now dialed up to 2000, which means that by the time it gets to the
end, it kind of flips around.
So let's, first of all, upright our car if it turns upside down so that it can actually keep driving.
So we're going to create a new script to put on our car.
So create a C-sharp script.
Let's call it Flip and then we'll open that up.
What it's going to do is to flip the car over if it's been upside down for a certain amount of time.
All right.
So we need the rigid body of the car.
So rigid body B and we're going to have a float last time checked.
Which will keep track of the last time we checked which way the car was actually facing.
Then the rigid body can go rb.
Equals this get component rigid body.
And we're talking about the rigid body that's on the body of the car because that's the main one.
We want to know whether it is upside down or not.
Okay.
And then we're going to create a new method called Right Car, which will turn the car upside down.
Okay.
So in here, let's put this dot transformed position plus equals vector three dot up that will move
the car up in the up direction just a little bit so that when you rotate the car over, it doesn't end
up under the ground.
And then we'll do a this dot transform dot rotation equals Catalonian dot look.
Rotation.
And if we look towards the forward vector, this dot transform forward, what it does is it flips the
car over and keeps it pointing in the direction that it was pointing.
So that's the code there to actually do it.
Now let's go in the update and execute it.
So we want to test if transform dot.
Up dot y.
So this is the y value of the up vector from the car.
Now if it happens to be greater than zero f, then we still have the car up the correct way.
And at the same time we want to check that the car is still actually moving.
So raise your body velocity magnitude.
If that is greater than one, the car is still moving and you can modify those values however you like.
If you want it to be lying upside down a little bit longer, you can change that in a moment.
But if you want these speeds, you want to actually still be like sliding along upside down.
You could, of course, make this a greater value or smaller value depending on what you want.
Now, this y value here is zero if you're upright, but because the car can actually sort of tip and
go over like undulating ground and go on its side, but not actually being flipped upside from testing,
we've actually found that 0.5 is a better value for checking in here.
Now, if it is, last time checked is going to be equal to time, time or the time that it was found
to still be upright.
So when the car is going okay, we're going to keep checking the time.
Now, if it's upside down, we'll stop checking the time.
So that's where we can go.
If time time is greater than the last time checked, plus our time out.
So how long do you want it to be?
Upside down?
3 seconds.
What are four?
3 seconds.
Then we will in here.
Just go right car which will turn it back up the other way.
Okay, so save that and we'll flip back into unity.
Let's go to our car and we want to drag and drop this flip script onto the car.
Now, remember, it's trying to get a rigid body that's going to tell it which way it's facing.
Very important.
The rigid body is on the same component or the same object that this script is on.
Okay, let's just select these wheels and have a look the settings, okay, we've got our max torque
is 2000 now.
Steering angle is 60.
Now if we actually make the stiffness on these turning frictions like let's set it to five should help
it grip and spin out a bit more easily.
And if we allow the steering to be greater too, let's set that to like 100 and let's have a look if
we can get this car to flip upside down.
Okay.
So in the scene, I'm just going to zoom out and try and watch it from above.
Okay.
Well, definitely a bit of flipping there because it keeps landing on its right side.
Here we go.
So now we're upside down into this corner and our anti flip bang puts us up the right way.
Of course, at that point, we're still going at the same sort of acceleration and all those sorts of
things.
So notice how immediately it gets completely out of control and we won't worry about that right at this
moment because we're going to make a few more tweaks that might calm that down.
And if it becomes an issue, then we can come back and address it a little later on down the track.