WEBVTT

00:06.430 --> 00:13.480
In this lecture, we're going to add a bit of code to stabilize a car and stop it from flipping over.

00:13.570 --> 00:26.800
So let's create a new C-sharp script and we'll call this anti roll and then let's open that up.

00:27.820 --> 00:32.740
And it might seem at this point that we're spending an awful lot of time on physics stuff for these

00:32.740 --> 00:40.450
cars, and I guess physics and I go hand in hand because a lot of the behaviors of objects in the real

00:40.450 --> 00:48.010
world are dependent on physics, especially when you come to moving things around with wheels and such.

00:48.040 --> 00:52.900
You've got to have your head around the physics as well so that you can control it with the eye.

00:53.170 --> 00:53.650
Okay.

00:53.650 --> 01:01.630
So first of all, we're going to create a public float, which will be our anti-roll amount, and we're

01:01.630 --> 01:04.210
going to set that to 5000.

01:05.740 --> 01:10.990
Then we want a public wheel collider.

01:11.470 --> 01:17.140
So we want our wheel left front.

01:17.590 --> 01:28.540
We have to keep track of which ones, which public wheel collider we'll write front and then Public

01:29.110 --> 01:36.340
Wheel Collider And we want our wheel left back.

01:36.820 --> 01:40.060
And then guess which one public?

01:41.900 --> 01:47.000
We'll collider we'll be right back.

01:47.570 --> 01:49.340
Then we also need the rigid body.

01:49.340 --> 01:53.950
So rigid body are be okay like that.

01:53.960 --> 01:56.840
Now we can grab the rigid body like we have before.

01:56.840 --> 02:07.220
So our B equals this dot get component, rigid body and then make sure we spell component correctly.

02:07.220 --> 02:10.190
Just up here, get rid of that.

02:10.670 --> 02:11.690
Okay.

02:11.720 --> 02:19.460
Next, we're going to write a method for grounding the wheels and we ground the front wheels and the

02:19.460 --> 02:21.410
back wheels at the same time.

02:21.410 --> 02:35.390
So we'll call it void ground wheels and this will be wheel collider, wheel lift wheel and wheel collider.

02:35.390 --> 02:36.740
Right wheel.

02:36.740 --> 02:38.780
So wheel right.

02:39.590 --> 02:46.850
Then we're going to get a wheel hit, hit, value float.

02:48.870 --> 02:50.000
Travel.

02:50.010 --> 03:00.150
L Travel left equals 1.0 f float travel right equals 1.0 f.

03:01.020 --> 03:01.510
Okay.

03:01.530 --> 03:04.200
Now what we're going to do, we'll use these values in a moment.

03:04.200 --> 03:08.220
But first of all, we want to figure out if these wheels are grounded.

03:08.220 --> 03:10.740
So bool grounded.

03:11.130 --> 03:19.890
Left equals wheel left dot get ground hit out hit.

03:19.950 --> 03:20.120
Okay.

03:20.160 --> 03:27.690
So you can actually ask the wheel if it's hitting something and then get this, we'll hit value here

03:27.690 --> 03:33.210
so we know that the wheel is on the ground and we say if grounded.

03:34.860 --> 03:45.690
L So if we are grounded then travel minus w dot transformed or inverse.

03:47.720 --> 03:49.880
Transform point.

03:52.430 --> 03:56.300
Hit dot point dot.

03:56.300 --> 03:57.260
Why?

04:00.490 --> 04:11.140
Minus the left shoulder radius divided by L suspension distance.

04:11.140 --> 04:23.050
And basically we want to move the wheel along the actual suspension so that we're not basically tipping.

04:23.050 --> 04:29.320
So as the car sort of tips over, you, imagine how a wheel actually sort of goes down to the ground

04:29.320 --> 04:35.470
on one side as it sort of extends out along its suspension.

04:35.650 --> 04:36.160
Okay.

04:36.160 --> 04:44.170
So our hit point here, that should be a bracket and that's the code there.

04:44.170 --> 04:46.030
So that's our left.

04:46.030 --> 04:49.750
Now we have to do exactly the same thing for our right.

04:49.750 --> 04:56.110
I'm going to grab all of this and put it down here.

04:56.110 --> 04:57.880
So we want grounded.

04:58.150 --> 05:04.270
Ah, this one is going to be w r this is grounded.

05:04.630 --> 05:16.990
Ah this is w r and then we want to make sure we've got W R and W down here to work out the travel distances

05:16.990 --> 05:17.860
of those.

05:18.970 --> 05:25.540
Just a little mistake we found as is inevitable when I copy and paste a block of code and then try and

05:25.570 --> 05:30.550
update everything, I miss something and you might have noticed this travel l here actually needs to

05:30.550 --> 05:38.590
be travel r so make sure in your code that it says travel R And I'll change it in mine down the track.

05:38.590 --> 05:41.080
But for this video it still look like travel.

05:41.080 --> 05:53.110
L Okay, we can then set up our anti-roll force that we want to add on to the car to stop it from rolling

05:53.110 --> 05:53.440
over.

05:53.440 --> 06:01.420
So float Anti-roll force is going to equal travel.

06:01.420 --> 06:12.760
L minus travel are multiplied by our anti roll value.

06:13.660 --> 06:14.020
Okay.

06:14.050 --> 06:18.280
And then down here we will apply that to our wheels.

06:18.280 --> 06:30.010
So if grounded l then our rigid body does not add force at position.

06:30.760 --> 06:41.260
We're going to add w l we're going to add w l dot transform up multiplied.

06:41.260 --> 06:48.700
And this is the direction that we're applying the force multiplied by minus anti-roll force because

06:48.700 --> 06:50.770
we want to go down, not up.

06:50.770 --> 06:59.860
That's just why we've got a minus in there and the position will transform position.

07:00.790 --> 07:03.640
Now we need to do the same thing to the right side.

07:03.970 --> 07:09.070
So we're going to copy that and put it down here and this will be grounded.

07:09.070 --> 07:16.690
R And we'll add the force to the right side of the vehicle using the up.

07:16.690 --> 07:21.910
And this time it's actually going to be turned around the other way, facing the other direction because

07:21.910 --> 07:23.710
of the side that it's on.

07:23.800 --> 07:27.940
And then we're going to put R in here.

07:28.090 --> 07:28.630
Okay?

07:28.630 --> 07:33.670
Now, once we've grounded the wheels or we've created our grand wheel method, we can actually call

07:33.670 --> 07:34.150
it in here.

07:34.150 --> 07:46.510
So ground wheels and this will be wheel left front and wheel right front.

07:47.190 --> 07:51.930
And then we do the same again for our back wheel.

07:51.930 --> 07:56.310
So we'll back left.

07:56.310 --> 07:58.130
So left back.

07:58.140 --> 07:58.590
Where is it?

07:58.590 --> 07:59.190
There.

07:59.580 --> 08:05.580
And then we'll write back like that.

08:06.120 --> 08:11.580
And on second thoughts, I'm going to put a minus in there because I'm not sure why I wouldn't would

08:11.580 --> 08:13.440
have had that in the first place, come to think of it.

08:13.490 --> 08:15.480
Okay, so let's save that.

08:16.170 --> 08:18.120
Go back into unity.

08:18.120 --> 08:22.860
We're going to put this script onto our car body.

08:22.860 --> 08:31.080
So this is the anti roll goes over here and then it needs our wheel colliders passed through again.

08:31.080 --> 08:35.790
So we want the wheel left front, which is this one.

08:36.090 --> 08:41.250
Then we want the right and then we want the rear one.

08:41.250 --> 08:49.950
So at the rear left, which is the left back and the rear back, which is the rear right.

08:50.160 --> 08:53.340
Now, once that's done, we're ready to test it out.

08:53.340 --> 08:58.980
Now, remember, the car flips over once it on around this circuit up onto that other one.

08:58.980 --> 08:59.200
Okay.

08:59.220 --> 09:06.720
So I'm going to press play and while it's getting over there, I'm going to move this around so we can

09:06.720 --> 09:10.680
see it when it actually gets over to that side.

09:15.380 --> 09:17.600
And what the effect is.

09:18.260 --> 09:23.480
And you might even want to increase the amount that you've got going on there.

09:23.930 --> 09:29.600
As far as the roll amount, which is this anti roll amount for 5000 here.

09:29.660 --> 09:30.080
Okay.

09:30.080 --> 09:32.280
So we're coming up to this corner.

09:32.300 --> 09:41.210
Let's have a look at the effect that that anti roll is now giving us just up here.

09:41.570 --> 09:47.860
So we go around the corner and notice how we went up a little bit, but we pushed it back down again.

09:47.880 --> 09:52.700
We didn't actually end up on the side of the car, which is what happened before.

09:52.700 --> 10:01.730
So we now have basically a script that is working out which wheels are basically sort of coming off

10:01.730 --> 10:08.960
the ground or slipping down low on their suspension and then adding force on that side to kind of push

10:08.960 --> 10:10.880
the car down on that side.

10:10.880 --> 10:22.760
So if the car, for example, here, if I can find it is is rotating and it's going like this, then

10:22.760 --> 10:28.190
these tires on the side are going to come down a little bit on the suspension, aren't they?

10:28.190 --> 10:31.640
And that means that car is obviously tipping that way.

10:31.640 --> 10:38.870
And that means we want to go like push the car down on this side to kind of keep it grounded so that

10:38.870 --> 10:40.850
you don't end up with it tipping over.

10:40.850 --> 10:46.490
And again, it's going to take a little bit of a tweaking these values to get exactly the right one

10:46.490 --> 10:47.270
for you.
