In this lecture, we're going to add a bit of code to stabilize a car and stop it from flipping over.
So let's create a new C-sharp script and we'll call this anti roll and then let's open that up.
And it might seem at this point that we're spending an awful lot of time on physics stuff for these
cars, and I guess physics and I go hand in hand because a lot of the behaviors of objects in the real
world are dependent on physics, especially when you come to moving things around with wheels and such.
You've got to have your head around the physics as well so that you can control it with the eye.
Okay.
So first of all, we're going to create a public float, which will be our anti-roll amount, and we're
going to set that to 5000.
Then we want a public wheel collider.
So we want our wheel left front.
We have to keep track of which ones, which public wheel collider we'll write front and then Public
Wheel Collider And we want our wheel left back.
And then guess which one public?
We'll collider we'll be right back.
Then we also need the rigid body.
So rigid body are be okay like that.
Now we can grab the rigid body like we have before.
So our B equals this dot get component, rigid body and then make sure we spell component correctly.
Just up here, get rid of that.
Okay.
Next, we're going to write a method for grounding the wheels and we ground the front wheels and the
back wheels at the same time.
So we'll call it void ground wheels and this will be wheel collider, wheel lift wheel and wheel collider.
Right wheel.
So wheel right.
Then we're going to get a wheel hit, hit, value float.
Travel.
L Travel left equals 1.0 f float travel right equals 1.0 f.
Okay.
Now what we're going to do, we'll use these values in a moment.
But first of all, we want to figure out if these wheels are grounded.
So bool grounded.
Left equals wheel left dot get ground hit out hit.
Okay.
So you can actually ask the wheel if it's hitting something and then get this, we'll hit value here
so we know that the wheel is on the ground and we say if grounded.
L So if we are grounded then travel minus w dot transformed or inverse.
Transform point.
Hit dot point dot.
Why?
Minus the left shoulder radius divided by L suspension distance.
And basically we want to move the wheel along the actual suspension so that we're not basically tipping.
So as the car sort of tips over, you, imagine how a wheel actually sort of goes down to the ground
on one side as it sort of extends out along its suspension.
Okay.
So our hit point here, that should be a bracket and that's the code there.
So that's our left.
Now we have to do exactly the same thing for our right.
I'm going to grab all of this and put it down here.
So we want grounded.
Ah, this one is going to be w r this is grounded.
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
of those.
Just a little mistake we found as is inevitable when I copy and paste a block of code and then try and
update everything, I miss something and you might have noticed this travel l here actually needs to
be travel r so make sure in your code that it says travel R And I'll change it in mine down the track.
But for this video it still look like travel.
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
over.
So float Anti-roll force is going to equal travel.
L minus travel are multiplied by our anti roll value.
Okay.
And then down here we will apply that to our wheels.
So if grounded l then our rigid body does not add force at position.
We're going to add w l we're going to add w l dot transform up multiplied.
And this is the direction that we're applying the force multiplied by minus anti-roll force because
we want to go down, not up.
That's just why we've got a minus in there and the position will transform position.
Now we need to do the same thing to the right side.
So we're going to copy that and put it down here and this will be grounded.
R And we'll add the force to the right side of the vehicle using the up.
And this time it's actually going to be turned around the other way, facing the other direction because
of the side that it's on.
And then we're going to put R in here.
Okay?
Now, once we've grounded the wheels or we've created our grand wheel method, we can actually call
it in here.
So ground wheels and this will be wheel left front and wheel right front.
And then we do the same again for our back wheel.
So we'll back left.
So left back.
Where is it?
There.
And then we'll write back like that.
And on second thoughts, I'm going to put a minus in there because I'm not sure why I wouldn't would
have had that in the first place, come to think of it.
Okay, so let's save that.
Go back into unity.
We're going to put this script onto our car body.
So this is the anti roll goes over here and then it needs our wheel colliders passed through again.
So we want the wheel left front, which is this one.
Then we want the right and then we want the rear one.
So at the rear left, which is the left back and the rear back, which is the rear right.
Now, once that's done, we're ready to test it out.
Now, remember, the car flips over once it on around this circuit up onto that other one.
Okay.
So I'm going to press play and while it's getting over there, I'm going to move this around so we can
see it when it actually gets over to that side.
And what the effect is.
And you might even want to increase the amount that you've got going on there.
As far as the roll amount, which is this anti roll amount for 5000 here.
Okay.
So we're coming up to this corner.
Let's have a look at the effect that that anti roll is now giving us just up here.
So we go around the corner and notice how we went up a little bit, but we pushed it back down again.
We didn't actually end up on the side of the car, which is what happened before.
So we now have basically a script that is working out which wheels are basically sort of coming off
the ground or slipping down low on their suspension and then adding force on that side to kind of push
the car down on that side.
So if the car, for example, here, if I can find it is is rotating and it's going like this, then
these tires on the side are going to come down a little bit on the suspension, aren't they?
And that means that car is obviously tipping that way.
And that means we want to go like push the car down on this side to kind of keep it grounded so that
you don't end up with it tipping over.
And again, it's going to take a little bit of a tweaking these values to get exactly the right one
for you.