In this lecture, we're going to construct our car or a simple car.
Now, I know we've already got a car model sitting in here.
If I can find it, let's just go over here.
So what we're going to do is actually use that instead of our single tire.
Now, I did mention, and you did see that your tire falls through the ground when it's a single wheel
collider and it's just being driven around.
But once we now attach that to the car, then that issue is going to go away.
So you can keep that tire if you want.
Just turn it off in the inspector and let's go over to our car.
Let me just shade this so I can actually see it.
And in this case, if you select the car, you'll see that it has a rigid body on it.
If we drop that down, the mass of this car is 1500 and then it has all the bits and pieces for the
visual part of the car, the body.
But then it's got all these wheels, so it's got a front left, front, right, rear left and rear right
wheel.
Now, these wheels are a little more complicated than the truck tire that we had, which was just the
one mesh.
If you have a look at them, the wheels themselves have nothing, sort of just a transform at the top.
Then there's your disc brake mesh renderer, and then the wheel renderer.
Let's just zoom in and grab hold of one of these.
Now, the object that you put the wheel collider on is the object that is going to rotate with that
wheel collider.
Therefore, this is the one you want to put the wheel collider on.
Okay.
So if we just open up all of these and shift select or command select, I should say, or control select
if you're on Windows and then we can drag and drop the drive script onto there and we can also add our
wheel.
Collider Okay.
Now we don't put a rigid body onto these wheels because it's the car itself is holding the rigid body
with the weight of the car.
Okay.
So now once we've got those, let me select them again.
We need to set up their positions because if you have a look at them, they're a little bit big.
So they're coming right down here, which is just way too big for us and we need to move them up as
well.
So let's move it up to 0.17 in the why I found is quite good.
And then the radius at the top here instead of 0.5, 0.45 is a bit better and you could even make it
a little bit more snug than that if you went 0.4, say four.
Let's have a look if that's a bit better, because remember, you want that point that you're applying
the force sitting down at the bottom of each tire.
Okay, so we've got that one.
And if we go and have a look at this one, it's all good.
And this one, it's all good as well.
Okay, great.
Now it's time to try this out.
So let's move out a little bit.
I'm going to grab the main camera and align to view.
So game object, align with view.
And now let's press play and have a look at driving our car so we can use the down arrow key to go backwards
and the up arrow key to apply forward force.
And you'll notice because we're applying a force, the car already has momentum as such applied to its
body.
So it's going to keep moving forward until the reversing force or the forward force actually then counterbalances
that.
Okay, great.
Now one thing you will notice is that the tires aren't turning around, but the colliders are.
If we select again, let's command or control, select our wheels and turn on the gizmos in here.
What you're going to see is them actually turning around.
Now, I might have to move my camera a little bit closer for you to be able to see those.
Let's do it over here and let's go back into my wireframe because you'll be able to see them way clearer.
So let's select them again.
Okay.
Now press play and you will see them turning around so you can see them there.
As they're moving.
So that's all working fine.
It's just that they don't automatically move the meshes to which they are attached.
That means we have to go back into our drive code and add a little bit of a tweak in here to move the
meshes around.
So inside of go right the bottom after we've actually moved the wheel collider itself, we can get the
wheel collider's pose in the world and apply it to our tires.
So to do that, we'll go turn in.
Quote, I'll just call that.
And then a vector three position and what we're going to do with our wheel colliders.
I will collider dot.
Get world POWs and our out will be our position.
And our out for cartooning.
So this is giving us the rotation and the position of the wheel collider which we can then use to orient
and position the actual mesh.
And we do that like this dot transform dot position equals the position we just calculated or got back
from our wheel.
Collider And then this dot transform rotation will equal the return in rotation that we've got out of
there as well.
All right.
So let's save this and we will go back into unity and press play and have a look at our tires going
around.
Right.
So a very interesting result.
And I bet you weren't expecting that.
So what's actually gone wrong with this?
Well, it's the way that we have this set up.
So we've got these wheel meshes with the colliders on them.
Now the code is moving the actual game object for the wheel that has the mesh on it, not the mesh itself.
And so when the wheel moves the wheel collider on, it is also affected.
And you get this sort of feedback loop between each one which makes it not operate all that well.
So what we need to do instead is to create a separate list of colliders that are in the car and keep
them separate from the actual meshes.
So what I'm going to do in here is actually under car.
I'm going to create an empty game object.
So we want an empty and let's call this colliders.
And then to the colliders, we're going to grab a copy of each of these.
So if we duplicate that, so control D and then just drag that down into here and we want this wheel
here, drag that down into there, this one duplicate, bring that down to there and this one duplicate
and bring that into here.
Okay.
So now that we've done that, we go back to our wheels that we've had before that have the mesh and
the collider on it.
And I'm going to shift select them.
Oh, sorry.
Control.
Select them or command, select them and remove.
The Wheel collider and the.
Drive script from them.
So they are just going to be the meshes.
Then inside of these colliders, if you select them, you'll see that you've ended up with all the code.
Obviously the drive needs to know about the actual mesh that we're wanting to affect.
So this here needs to be aligned with that.
We'll there.
So let's go back into our drive script and then at the top here, we're going to feed that in.
So we want a public.
Game object.
We'll mesh, which we will set.
And then down here in our go, it's not this dot transform that's being affected.
It's going to be the wheel mesh.
So this will be we'll mesh like that.
Okay, so save that script.
Let's go back into the drive.
Now we want to select the front left and we want to grab the front left mesh model here, this one and
drag and drop that down into the we'll mesh for it and then we want our front right one do the same
with that one.
Do make sure you get the correct ones lined up with each other because then you'll have the wrong collider
affecting the wrong actual mesh.
And then the last one is the rear.
Right.
I'll bring that down into there.
Okay, great.
So now we're ready for another test drive.
So press play.
And now if you drive, you'll see that your wheels are actually moving.
Now I'm seem to have an error that I haven't picked up something.
So the wheel collider attached to wheel two which is the right left two.
Now do make sure you double check your own settings in here.
The colliders are the wheel parts that will have the drive script and the wheel collider on them.
Also make sure there's only one copy of each of those on there because it might have been duplicated
and also remove the mesh filter and the mesh render from each of those colliders and the other ones.
The meshes make sure that they don't have any of the wheel colliders or the drive script on them, but
you are assigning them to the wheel mesh exposed variable on the colliders.
So the wheel mesh itself will look like this in the inspector.
It has a mesh filter and a mesh renderer and then the Associated Collider with it, which this one here
has no mesh filter, no mesh renderer.
You can see I remove those and then it has the wheel collider and the drive script and the drive script
is assigned with the mesh that it associates it.
So this has been dragged and put into there.
Now that we've made those changes to the car prefab you want to select the car and over in the inspector
go to overrides and right down the bottom you'll find apply all which will save all those settings back
so down there okay so now when we press play and there we go we have a fully functioning car, well,
at least you can go forward and backwards.
Right.
So when we come back in the next lecture, we'll start looking at how we can turn the car.