Before you can start creating any racing game where you're going to be relying on physics, you need
to understand what's underlying the system.
And it's not only the overall physics system, but what really is and literally is driving it are the
wheels of your karts and the physics that is on those wheels.
And the way you set that up is absolutely fundamental to getting your game correct.
Now, if you've had any experience using the wheel colliders and the vehicles set up in unity, you'll
know that it's seemingly very unpredictable, especially if you don't know where to start or what exactly
you're trying to achieve.
So in this lecture, we're going to go right back to basics and look at just a single wheel before we
start looking at the eye control code to drive a car around.
We're going to, first of all, set up a car with the physics system from basics.
So attach this lecture is a starter package, bring that in to unity and you'll see that I've opened
it up here inside of the car I seen you'll find a truck tire model that I put in there.
And also this car model, which you might recognize from an old unity project where it had car racing
and quite like this model.
So we're going to use that.
But first of all, we're going to use the truck tire.
So let's focus on that truck tire.
The basis of the mechanics of the physics for a car is all driven by the wheels.
Now, the most important thing, if you want to apply this technique to other models of cars that you
have bought from the assets or you've made yourself, the wheels have to be obviously a separate object.
They can't be sort of part of the car mesh because they have to move independently.
The other thing that's important is the direction of your access system or your pivot point for your
wheel model.
So the forward direction of it must be the forward vector, which is your Z vector that way.
And the outward facing vector has to be the X, so it has to go this way.
Now, you could remodel this tire for the other side of the car so that the red axis is pointing out.
But there's no need to do that if you duplicate it and just move it over like this and then just hit
the arc to rotate it around and just put it there.
So now when we select it and hit the W, we'll see that.
We've actually got a forward direction and it's outward is that way.
Okay.
So that's most important so that you don't have four different tire models and everything is facing
the right way.
So if we take a look at the truck tyre, first of all, let me just delete that second one.
I made this one here.
It is a single mesh all by itself.
And that is important, as I just said.
But it has no children, things going on whatsoever.
What we're going to do with it is make it into a physics object using a wheel collider and a rigid body.
So we add in the inspector a rigid body, and then we also add a component for a wheel collider.
Now, once you've done that, you're going to see over in the same view a green shape around it, which
defines where that wheel is.
Let's see if I can put myself into just wireframe mode.
Actually, I might go shaded wireframe.
That might help you see it a bit.
A little bit better?
No, not necessarily.
Let's just go into wireframe instead.
Okay.
So if we select the wheel now, it's going to be a little hard for you to see on my screen.
If you follow along, you'll be able to recognize this is the collider that's going around here.
You've then got this spring value that the tire can move along and it basically has to align with the
actual model of the tire which is around here.
So what are we going to do?
Let me go back into shaded is we're going to grab hold of that wheel collider and actually move it up
in the Y direction.
So might need to just bring it up a little bit.
So I might have to type it in 0.5.
It's going to be way too high .1.2.
So 0.15 is about right, I think.
Let's see if we can see it.
Okay.
So you can see it there around that outside part and it's got about the same distance all the way around
there.
Okay.
Let's also while we're here, we're going to just zoom out a little bit and get our main camera and
make sure that it's focused on that tire so we can see what it's going to do and react.
Okay.
So we're going to go align with The View and now what we can do with our tire is if we just grab it
and lift it up in the air a little bit and then press play, you'll see the physics on it start to work.
Okay, so, whoa, what happened to the tire?
Let's just zoom back again and I'm just going to play it again because the tire will come back down
again.
But what happens is it's bouncing on the ground and then it's going up and there it is.
There it's going to get up as far as it can with that bounce and it's going to come back down, hit
the ground.
And when it does, it'll have even more bounce.
And you saw that zoom away really quickly just then.
Why is this happening?
Well, the tire physics is meant to be holding the load of a vehicle.
Okay.
Now the actual mass of that vehicle, if we have a look over in the inspector, you'll see that the
rigid body has a mass.
Now, this is the mass that you want to load onto your tire.
This basically represents one kilogram.
Cars do not weigh one kilogram.
The wheel collider is working on a wheel mass of 20 kilograms, which is probably about right for your
average car tire.
But for the car itself, you probably want something more like 1000 kilograms in that mass, which will
then give you better reactions to the physics because you're not reacting to like holding up a one kilo
block on this tire when it bounces.
So now if we press play and have a look, we will get a much more respectable tire.
And you saw that there.
Okay, let's zoom in on this truck tire again and I'm going to try this wire frame thing should be able
to see it a little bit better.
If we turn off the lighting, it doesn't seem to make much difference, but now it's aligned.
It's actually a bit easier for you to see.
Okay, so what we've got with this tire is basically this orange bit is your suspension string.
Now this is the center of the wheel collider and what it's trying to do in the physics system is to
get this center part of the tire to the desired location along this spring.
Now, how do we set that desired position at the moment?
We have this spring that springs in both directions.
If you come over to your wheel collider and have a look at the suspension spring and the target position
you've got.
Point five.
So what happens when this now drops is that it allows this part of the collision when this thing actually
hits the ground to spring up along this spring and then sort of bounce back down again.
And we can have a better look at that if I then move my main camera again, align with view.
And what I'm going to do is find my gizmo setting, which is just here, make sure that's turned on,
and then you can select your tire and you'll be able to see that actual spring.
Now let's just move the camera out a little bit and what's going to be in the X direction there.
Okay.
And we'll select the tile.
Let's have a look at how it reacts on that spring.
So you'll watch that center line that actually now comes down and it bounces up to there that you might
have seen and then stays within that spring.
So what do you think's going to happen if we take that center line and actually put it at the top of
the spring?
So if we go over to our wheel collider and change that target position to one K, notice that the target
position, which is the center of the collider, is now sitting very much at the extreme top of that
spring.
So what's going to happen?
Let's press play now.
It's going to come down and bang, just doesn't do anything.
So what you have to imagine is now happening is that this whole collider is trying to move down, but
when it hits the ground, it wants to bounce up, or at least that's its reaction.
But it's already sitting at the top of the spring, so therefore it doesn't really have anywhere else
to go.
Okay.
Now, one last thing that's quite important in this.
And first of all, let's just set the target position back to 0.5 so that we have our correct tire.
Is this position down here?
Okay.
So this little green sphere, so what that is, is the force applied position so that when we apply
a rigid body force to our wheel, this is the point that will get affected and of course cause our wheel
to turn around.
How is that set?
Well, you can see it's set perfectly on the outside here.
If for some reason you wanted to apply the force down in here inside of the tire instead of along here,
then you've just got to change this force applied position distance, which is at zero.
If we put that up to one, you'll see that it allows you to apply the force at the very top of the tire.
But because of the way we're creating these tires and the fact that it's actually going to be a ground
driving vehicle, we want it to stay down there.
Right.
So that's a real quick overview of the wheel collider physics set up.
In the next video, we'll come back and actually start moving this around with some physics.