Isaac Newton came up with a set of three laws of motion.
The most famous is that of f equals ma or force equals mass multiplied by acceleration.
Or more importantly for us, the rearrangement of acceleration equals force divided by mass.
This is extremely important to keep in mind, especially when designing vehicle physics, because what
it means is that to accelerate a mass, the larger the mass, the more force required, and that the
same force on different masses will accelerate differently.
For example, given a car with weight 1000 kilograms and a force of 5000 Newtons, you can obtain an
acceleration of five meters per second squared.
The same force on a 100 kilogram car would result in acceleration of 50 meters per second squared.
What does that mean for our wheel suspension?
Well, we've a spring with an upward force acting on a particular mass.
The greater the mass, the more force the spring requires to give the same upward acceleration to the
car, the bigger the mass, the more compressed the spring.
However, in a vehicle, there are also many other forces to consider.
First, we have the dampening force that we can set that represents a downward force that makes the
spring much more rigid and less likely to bounce.
It slows down its recoil.
And then you also have gravity acting on the car and pushing down on the spring.
So the spring needs to combat both gravity and dampening to give the same effect on different masses.
What does all this mean for you?
Well, what I'm trying to get across, I guess, is that when you start programming the physics in these
systems and working with suspension and springs and different masses that you keep in mind your own
real world experience, you know, you know logically and instinctually how physics works because you're
in that world constantly, all day.
Therefore, don't let your intuition go out the window when you start programming this stuff because
it's all programmed based on real life physics.
So keeping all that in mind and what we've learnt so far about Wheels, I've got a little bit of a challenge
for you, so I've created two duplicates of the tire that we were working with before and just put them
on either side.
So this is the original tire and these are two here.
Now, when I press play, I want to show you what I get.
So what I want you to attempt in this challenge, and that is to make bouncy tires with different suspension
setups.
Now, of course, this isn't going to actually do anything because I need to lift them up into the air.
So let me select all of those tires and I'll just lift them up like this.
Okay.
And press play.
Okay.
We've got a pretty bouncy one over here, a not so bouncy one in the middle, which is the one we had
by default.
And then this one here, which is slightly different to that.
Now, your tires will go through the ground at some point.
Don't worry about that.
We'll fix that up a little bit later.
Let's have another look at the difference between these two here.
Okay.
So this one actually has a softer landing than this one.
Okay.
So there's a reason for that.
So pause the video now.
Give it a go of changing these values.
And when we come back, I'll go through the settings that I've got for these.
Okay.
How did you go with that?
Let's have a look at this tire here, the one on the left.
Now, you'll notice if I zoom in on it that the collider I've realigned perfectly and I've made the
spring way longer.
That means that as the tire is actually falling, it's being compressed down on this spring.
That's why it falls slower, because it's taking that spring as its lead time.
Now, if we have a look over in the settings for it, in order to set it up like this, I had to increase
the suspension distance to to.
Now when you do that, the offset for the center of the wheel actually changes.
So we have it at what, 0.15 for the other default tire, I believe.
Let's have a look at that.
Yep.
And so what it's going to do is actually move that when you change the size of your suspension because
it needs to put the collider basically in that sort of center part there.
So we need to actually move this up.
So if we put it back to one, then that's going to sit it around the toy.
You always want it to be around the tire.
So the physics looks like it's happening to the actual mesh itself.
If it's not, it doesn't look like the mesh is the thing that is having physics acted upon it because
it's all real fakery, isn't it?
The only thing we're really doing anything to is the collider in the rigid body.
The mesh is just going along for the ride.
Let's have a look at the other one, this one here, which was much bouncier.
So over in the inspector, the suspension is one for that.
And you can see that the spring for the tire is the entire size of the tire or the diameter of the tire.
Now, when I set that to one, I had to change the center offset 2.5.
So it lined up again.
I've also turned the damper down to 450.
The other tires dampers were 4500.
Now what does that mean?
Well, the higher the dampening on it, then the greater the force is pushing back against the spring.
So you can imagine the dampening as it gets higher, you get a more rigid or firm spring.
It's not such a like really, really bouncy spring.
So that's why this tire over here, this one here is actually quite bouncy because we've turned that
dampening right down.
So it's like a car that's got really bad, worn out suspension.
The dampening on those suspensions, you know, it wears down over time, which is why you have to get
those repaired.
Okay.
So moving on, we're now going to write some script with some physics in it to move the tire.
So first of all, I'm actually just going to delete my extra tires that I created and then grab that
tire that we've got left and we'll bring it down onto the ground because it needs to drive on the ground
just there.
And let's go ahead and create a new script down in our assets folder.
So C-sharp script, let's call it drive and let's open that up, right?
So this drive script will go onto each wheel.
That means that the game object holding onto it, the collider that is, needs to have access to it.
So we want a public wheel.
Collider.
WC And then we also want the amount of force that we're going to add.
So public float and that will be talk.
Equals 200.
Now what is talk?
Will Talk is basically the rotational force and it's calculated using the pivot point.
So that's the point that our tyre is being rotated around or the centre of that collider at this point
and the point of force, which is where that little sphere is that I showed you earlier.
So the distance or the vector from the pivot point to the point of force application is our rotational
vector, and then the actual force that gets applied is applied in a straight line.
So it's the equivalent of normal force, linear force, if you just like pushing something forward because
basically what you're doing as you're rotating the tire is you're constantly pushing it forward.
But that sort of pushing it around because it's sort of stuck to that pivot point.
And so torque is actually the cross product of both of those vectors, R and F, if you're interested
on how that actually gets calculated, we don't need to calculate that.
We just have to set an actual torque amount.
Okay, so let's finish writing this code.
First of all, we're going to get our wheel collider so WC equals this dot get component wheel collider
like that.
And then in our update we're going to use the vertical axis to get an acceleration amount.
So we'll go float a equals input dot get axis and the axis will be the vertical one.
Now this is the up and down arrow key.
So for going forward and backwards and then we'll pass that through to a method that's going to actually
drive the tire forward.
So void go and we'll send through float for acceleration and then in here will put acceleration equals
math f dot clamp and we'll clamp the acceleration between minus one and one, which it should be anyway,
because of this vertical axis that we're passing in, then we'll do a float for thrust torque, which
equals the acceleration multiplied by torque and then we actually apply that to our wheel.
Collider So WC motor torque, which is one of the variables within your wheel collider equals the thrust
torque like that.
Right now, once you've done that, all we need to do is call our go method sending through the acceleration
value that we're getting like that.
Okay, so save this switch back to unity.
And with your truck tire, you need to grab that script and drag and drop it onto the inspector so that
it's now attached to the tire.
And when we press play now, you'll find that you should be able to use your arrow keys.
Don't worry, I know it disappears.
Let's just have a look again as soon as you start moving.
Yep.
Okay.
You use your arrow keys, but you could see the start spinning around and then it falls through the
ground again.
Don't worry about that.
It's going to get fixed shortly.
When we come back in the next lecture, we're actually going to attach our wheel to a makeshift car
and start driving it around.