WEBVTT

00:06.820 --> 00:09.640
In the previous video we added in breaking.

00:09.640 --> 00:16.000
Now we're going to actually look at the forces that are on a car and how they affect the car's performance

00:16.000 --> 00:19.330
and how it can actually play around with those in the wheel.

00:19.330 --> 00:20.440
Collider.

00:20.560 --> 00:23.620
So we've got this car and it's breaking in.

00:23.620 --> 00:27.820
In the previous example, it wasn't actually too bad the way it got around the track.

00:27.820 --> 00:38.230
If we increased the max torque here to say, Oh, fast 2000 and then we pressed play, and if you do

00:38.230 --> 00:43.450
that, don't forget, you have to select all of the wheels on that particular car to do it.

00:43.480 --> 00:47.080
Now, what happens to this car as we approach the corner?

00:47.200 --> 00:52.960
Our braking distance is way too short for the speed that we're going and we end up flipping the car

00:52.960 --> 00:53.530
over.

00:53.770 --> 00:59.650
But the other thing is that now the car's wheels will actually slide.

00:59.650 --> 01:07.150
So you can see he actually slid out of this corner and again rolled over and we'll fix that a little

01:07.150 --> 01:08.050
bit later.

01:08.050 --> 01:14.710
But you've got this slippage that goes on as well, and they're actually controlled by these different

01:14.770 --> 01:18.250
friction values that we've got in the wheel.

01:18.250 --> 01:26.170
Collider So let's pause for a moment and just talk about those particular friction forces.

01:26.170 --> 01:32.560
The mechanics of friction on real rubber wheels is such that as the force is increasing, the slip stays

01:32.560 --> 01:33.820
relatively low.

01:33.820 --> 01:41.200
However, at some point called the extreme, the slip increases and thus the force is reduced.

01:41.530 --> 01:48.430
It continues to reduce to the point that the force remains constant and the slip just increases.

01:48.460 --> 01:53.700
Now remember that it's the torque being applied that increases the force on the tire.

01:53.710 --> 02:00.160
So this graph is saying that a torque can be applied until a maximum force is hit, at which the car

02:00.160 --> 02:03.460
will start to slip much more than it did before.

02:03.790 --> 02:10.540
If you applied a great deal of initial torque such that the force on the tire immediately exceeds the

02:10.540 --> 02:14.350
extreme, then you'll get an awful lot of slip.

02:14.560 --> 02:18.980
Again, to understand this, go back to what you know of the real world.

02:19.000 --> 02:24.250
If you start to move a car forward slowly, the wheels aren't going to slip.

02:24.280 --> 02:30.400
However, if you floor the accelerator and immediately apply a great deal of force to the wheels, then

02:30.400 --> 02:32.110
they'll start to spin out.

02:33.210 --> 02:40.350
Now these values here can be a little tricky to set, and we're not going to really go into the dynamics

02:40.350 --> 02:46.830
of these, but do feel free if you want to create sort of different cars with different friction values

02:46.830 --> 02:53.970
that you can play with these and possibly multiply the number of cars you've got of the same set up

02:53.970 --> 02:55.710
and then change these values.

02:55.710 --> 03:00.870
But what I will show you is the friction, stiffness, and that's the one that's going to give you the

03:00.870 --> 03:04.430
most sort of instant change in slippage.

03:04.440 --> 03:11.190
If we set the stiffness here to zero for the forward friction and I'm looking at my car in this scene

03:11.190 --> 03:17.100
sideways, what do you think is actually going to happen if you've got no friction on the wheels, which

03:17.100 --> 03:19.680
means it's not going to grip whatsoever.

03:19.680 --> 03:26.100
If we press play in this case, we will find that.

03:26.910 --> 03:29.300
It's trying to go somewhere, but it can't.

03:29.310 --> 03:33.180
The wheels are just going around and constantly slipping, basically on the spot.

03:33.180 --> 03:34.980
It wants to go so fast.

03:35.100 --> 03:41.700
So if we then turn this up to one, then we've got a higher stiffness.

03:41.700 --> 03:45.310
But you can actually keep increasing up towards infinity.

03:45.330 --> 03:52.050
Now, if you turn these stiffness values up a lot so that there's a lot of grip, then you've got more

03:52.050 --> 03:59.160
chance of rolling the car over when you go around the corner, because it's not like slipping around

03:59.160 --> 04:03.630
the corner and you might want it to sort of slip around the corner rather than roll.

04:03.630 --> 04:10.860
So it's going to be a matter of finding that sweet spot in the stiffness values for the exact type of

04:10.860 --> 04:13.080
effect that you're after.

04:13.110 --> 04:19.440
Now, this current car, the way we've got it, is actually slipping a little bit around the corners.

04:19.440 --> 04:24.360
But we've got this maximum torque now dialed up to 2000, which means that by the time it gets to the

04:24.360 --> 04:26.640
end, it kind of flips around.

04:26.850 --> 04:35.790
So let's, first of all, upright our car if it turns upside down so that it can actually keep driving.

04:36.950 --> 04:40.360
So we're going to create a new script to put on our car.

04:40.370 --> 04:43.040
So create a C-sharp script.

04:43.040 --> 04:47.710
Let's call it Flip and then we'll open that up.

04:47.720 --> 04:55.630
What it's going to do is to flip the car over if it's been upside down for a certain amount of time.

04:55.700 --> 04:56.150
All right.

04:56.150 --> 04:59.870
So we need the rigid body of the car.

04:59.870 --> 05:10.160
So rigid body B and we're going to have a float last time checked.

05:12.900 --> 05:17.520
Which will keep track of the last time we checked which way the car was actually facing.

05:17.700 --> 05:20.520
Then the rigid body can go rb.

05:21.790 --> 05:25.900
Equals this get component rigid body.

05:27.720 --> 05:34.680
And we're talking about the rigid body that's on the body of the car because that's the main one.

05:34.680 --> 05:37.080
We want to know whether it is upside down or not.

05:37.260 --> 05:37.850
Okay.

05:37.860 --> 05:44.820
And then we're going to create a new method called Right Car, which will turn the car upside down.

05:45.210 --> 05:45.630
Okay.

05:45.630 --> 05:57.960
So in here, let's put this dot transformed position plus equals vector three dot up that will move

05:57.960 --> 06:04.770
the car up in the up direction just a little bit so that when you rotate the car over, it doesn't end

06:04.770 --> 06:05.880
up under the ground.

06:05.880 --> 06:14.640
And then we'll do a this dot transform dot rotation equals Catalonian dot look.

06:16.080 --> 06:16.770
Rotation.

06:16.770 --> 06:22.440
And if we look towards the forward vector, this dot transform forward, what it does is it flips the

06:22.440 --> 06:27.630
car over and keeps it pointing in the direction that it was pointing.

06:27.750 --> 06:30.480
So that's the code there to actually do it.

06:30.480 --> 06:32.880
Now let's go in the update and execute it.

06:33.180 --> 06:38.580
So we want to test if transform dot.

06:39.600 --> 06:41.670
Up dot y.

06:41.670 --> 06:46.970
So this is the y value of the up vector from the car.

06:46.980 --> 06:55.980
Now if it happens to be greater than zero f, then we still have the car up the correct way.

06:55.980 --> 06:59.550
And at the same time we want to check that the car is still actually moving.

06:59.550 --> 07:03.210
So raise your body velocity magnitude.

07:03.210 --> 07:10.890
If that is greater than one, the car is still moving and you can modify those values however you like.

07:11.250 --> 07:17.190
If you want it to be lying upside down a little bit longer, you can change that in a moment.

07:17.190 --> 07:22.860
But if you want these speeds, you want to actually still be like sliding along upside down.

07:22.860 --> 07:28.080
You could, of course, make this a greater value or smaller value depending on what you want.

07:28.110 --> 07:35.910
Now, this y value here is zero if you're upright, but because the car can actually sort of tip and

07:35.910 --> 07:42.750
go over like undulating ground and go on its side, but not actually being flipped upside from testing,

07:42.750 --> 07:46.830
we've actually found that 0.5 is a better value for checking in here.

07:46.890 --> 07:56.010
Now, if it is, last time checked is going to be equal to time, time or the time that it was found

07:56.010 --> 07:57.600
to still be upright.

07:57.810 --> 08:02.460
So when the car is going okay, we're going to keep checking the time.

08:02.460 --> 08:05.520
Now, if it's upside down, we'll stop checking the time.

08:05.760 --> 08:07.020
So that's where we can go.

08:07.020 --> 08:15.120
If time time is greater than the last time checked, plus our time out.

08:15.120 --> 08:16.350
So how long do you want it to be?

08:16.350 --> 08:16.830
Upside down?

08:16.830 --> 08:17.940
3 seconds.

08:17.940 --> 08:18.390
What are four?

08:18.390 --> 08:19.320
3 seconds.

08:19.530 --> 08:21.720
Then we will in here.

08:21.720 --> 08:27.390
Just go right car which will turn it back up the other way.

08:27.420 --> 08:30.900
Okay, so save that and we'll flip back into unity.

08:31.050 --> 08:39.120
Let's go to our car and we want to drag and drop this flip script onto the car.

08:39.150 --> 08:43.500
Now, remember, it's trying to get a rigid body that's going to tell it which way it's facing.

08:43.500 --> 08:44.690
Very important.

08:44.710 --> 08:51.240
The rigid body is on the same component or the same object that this script is on.

08:51.660 --> 08:58.080
Okay, let's just select these wheels and have a look the settings, okay, we've got our max torque

08:58.110 --> 08:59.640
is 2000 now.

08:59.640 --> 09:01.470
Steering angle is 60.

09:01.500 --> 09:11.070
Now if we actually make the stiffness on these turning frictions like let's set it to five should help

09:11.070 --> 09:13.350
it grip and spin out a bit more easily.

09:13.350 --> 09:21.270
And if we allow the steering to be greater too, let's set that to like 100 and let's have a look if

09:21.270 --> 09:24.120
we can get this car to flip upside down.

09:24.420 --> 09:24.750
Okay.

09:24.750 --> 09:31.260
So in the scene, I'm just going to zoom out and try and watch it from above.

09:32.950 --> 09:33.350
Okay.

09:33.720 --> 09:40.380
Well, definitely a bit of flipping there because it keeps landing on its right side.

09:40.380 --> 09:40.800
Here we go.

09:40.800 --> 09:47.940
So now we're upside down into this corner and our anti flip bang puts us up the right way.

09:47.940 --> 09:54.180
Of course, at that point, we're still going at the same sort of acceleration and all those sorts of

09:54.180 --> 09:54.450
things.

09:54.450 --> 10:02.220
So notice how immediately it gets completely out of control and we won't worry about that right at this

10:02.220 --> 10:05.700
moment because we're going to make a few more tweaks that might calm that down.

10:05.700 --> 10:10.590
And if it becomes an issue, then we can come back and address it a little later on down the track.
