WEBVTT

00:06.360 --> 00:11.850
In this lecture, we're going to construct our car or a simple car.

00:11.880 --> 00:16.700
Now, I know we've already got a car model sitting in here.

00:16.710 --> 00:19.130
If I can find it, let's just go over here.

00:19.140 --> 00:24.040
So what we're going to do is actually use that instead of our single tire.

00:24.060 --> 00:29.490
Now, I did mention, and you did see that your tire falls through the ground when it's a single wheel

00:29.490 --> 00:32.460
collider and it's just being driven around.

00:32.460 --> 00:37.830
But once we now attach that to the car, then that issue is going to go away.

00:37.920 --> 00:40.470
So you can keep that tire if you want.

00:40.470 --> 00:44.790
Just turn it off in the inspector and let's go over to our car.

00:44.790 --> 00:47.460
Let me just shade this so I can actually see it.

00:47.640 --> 00:54.420
And in this case, if you select the car, you'll see that it has a rigid body on it.

00:54.420 --> 01:00.690
If we drop that down, the mass of this car is 1500 and then it has all the bits and pieces for the

01:00.690 --> 01:02.190
visual part of the car, the body.

01:02.190 --> 01:10.230
But then it's got all these wheels, so it's got a front left, front, right, rear left and rear right

01:10.230 --> 01:10.920
wheel.

01:11.310 --> 01:17.310
Now, these wheels are a little more complicated than the truck tire that we had, which was just the

01:17.310 --> 01:18.840
one mesh.

01:18.990 --> 01:25.560
If you have a look at them, the wheels themselves have nothing, sort of just a transform at the top.

01:25.560 --> 01:31.260
Then there's your disc brake mesh renderer, and then the wheel renderer.

01:31.260 --> 01:34.980
Let's just zoom in and grab hold of one of these.

01:34.980 --> 01:42.810
Now, the object that you put the wheel collider on is the object that is going to rotate with that

01:42.810 --> 01:43.440
wheel collider.

01:43.440 --> 01:48.000
Therefore, this is the one you want to put the wheel collider on.

01:48.120 --> 01:48.570
Okay.

01:48.570 --> 01:58.290
So if we just open up all of these and shift select or command select, I should say, or control select

01:58.290 --> 02:04.920
if you're on Windows and then we can drag and drop the drive script onto there and we can also add our

02:04.920 --> 02:05.370
wheel.

02:05.370 --> 02:07.200
Collider Okay.

02:07.200 --> 02:14.250
Now we don't put a rigid body onto these wheels because it's the car itself is holding the rigid body

02:14.250 --> 02:16.050
with the weight of the car.

02:16.230 --> 02:16.710
Okay.

02:16.710 --> 02:20.040
So now once we've got those, let me select them again.

02:20.040 --> 02:25.170
We need to set up their positions because if you have a look at them, they're a little bit big.

02:25.710 --> 02:31.710
So they're coming right down here, which is just way too big for us and we need to move them up as

02:31.710 --> 02:32.010
well.

02:32.010 --> 02:37.710
So let's move it up to 0.17 in the why I found is quite good.

02:37.710 --> 02:47.340
And then the radius at the top here instead of 0.5, 0.45 is a bit better and you could even make it

02:47.340 --> 02:54.150
a little bit more snug than that if you went 0.4, say four.

02:54.750 --> 02:59.700
Let's have a look if that's a bit better, because remember, you want that point that you're applying

02:59.700 --> 03:03.810
the force sitting down at the bottom of each tire.

03:03.990 --> 03:05.970
Okay, so we've got that one.

03:05.970 --> 03:09.990
And if we go and have a look at this one, it's all good.

03:10.590 --> 03:15.090
And this one, it's all good as well.

03:16.500 --> 03:17.640
Okay, great.

03:17.640 --> 03:19.860
Now it's time to try this out.

03:19.860 --> 03:22.620
So let's move out a little bit.

03:22.620 --> 03:25.980
I'm going to grab the main camera and align to view.

03:25.980 --> 03:28.980
So game object, align with view.

03:29.790 --> 03:37.680
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

03:37.680 --> 03:41.490
and the up arrow key to apply forward force.

03:41.490 --> 03:46.740
And you'll notice because we're applying a force, the car already has momentum as such applied to its

03:47.040 --> 03:47.300
body.

03:47.310 --> 03:53.640
So it's going to keep moving forward until the reversing force or the forward force actually then counterbalances

03:53.640 --> 03:54.630
that.

03:54.900 --> 03:55.770
Okay, great.

03:55.770 --> 04:02.220
Now one thing you will notice is that the tires aren't turning around, but the colliders are.

04:02.250 --> 04:11.790
If we select again, let's command or control, select our wheels and turn on the gizmos in here.

04:11.790 --> 04:14.890
What you're going to see is them actually turning around.

04:14.890 --> 04:18.630
Now, I might have to move my camera a little bit closer for you to be able to see those.

04:18.780 --> 04:24.990
Let's do it over here and let's go back into my wireframe because you'll be able to see them way clearer.

04:25.560 --> 04:27.360
So let's select them again.

04:29.260 --> 04:29.720
Okay.

04:29.740 --> 04:37.390
Now press play and you will see them turning around so you can see them there.

04:38.900 --> 04:39.910
As they're moving.

04:39.920 --> 04:41.410
So that's all working fine.

04:41.420 --> 04:46.250
It's just that they don't automatically move the meshes to which they are attached.

04:46.250 --> 04:52.490
That means we have to go back into our drive code and add a little bit of a tweak in here to move the

04:52.490 --> 04:53.510
meshes around.

04:53.510 --> 05:00.710
So inside of go right the bottom after we've actually moved the wheel collider itself, we can get the

05:00.710 --> 05:05.150
wheel collider's pose in the world and apply it to our tires.

05:05.330 --> 05:07.730
So to do that, we'll go turn in.

05:10.350 --> 05:12.420
Quote, I'll just call that.

05:12.420 --> 05:18.270
And then a vector three position and what we're going to do with our wheel colliders.

05:18.270 --> 05:21.180
I will collider dot.

05:22.080 --> 05:28.380
Get world POWs and our out will be our position.

05:30.240 --> 05:32.120
And our out for cartooning.

05:32.130 --> 05:39.840
So this is giving us the rotation and the position of the wheel collider which we can then use to orient

05:39.840 --> 05:42.180
and position the actual mesh.

05:42.180 --> 05:52.920
And we do that like this dot transform dot position equals the position we just calculated or got back

05:52.920 --> 05:53.670
from our wheel.

05:53.670 --> 06:02.430
Collider And then this dot transform rotation will equal the return in rotation that we've got out of

06:02.430 --> 06:03.290
there as well.

06:03.300 --> 06:03.630
All right.

06:03.630 --> 06:13.950
So let's save this and we will go back into unity and press play and have a look at our tires going

06:13.950 --> 06:14.700
around.

06:16.290 --> 06:16.830
Right.

06:16.830 --> 06:18.930
So a very interesting result.

06:18.930 --> 06:21.570
And I bet you weren't expecting that.

06:21.810 --> 06:25.770
So what's actually gone wrong with this?

06:26.580 --> 06:29.220
Well, it's the way that we have this set up.

06:29.220 --> 06:34.250
So we've got these wheel meshes with the colliders on them.

06:34.260 --> 06:43.260
Now the code is moving the actual game object for the wheel that has the mesh on it, not the mesh itself.

06:43.260 --> 06:49.530
And so when the wheel moves the wheel collider on, it is also affected.

06:49.530 --> 06:55.650
And you get this sort of feedback loop between each one which makes it not operate all that well.

06:55.650 --> 07:05.280
So what we need to do instead is to create a separate list of colliders that are in the car and keep

07:05.280 --> 07:07.050
them separate from the actual meshes.

07:07.050 --> 07:12.180
So what I'm going to do in here is actually under car.

07:12.180 --> 07:14.790
I'm going to create an empty game object.

07:14.790 --> 07:19.710
So we want an empty and let's call this colliders.

07:20.730 --> 07:26.370
And then to the colliders, we're going to grab a copy of each of these.

07:26.370 --> 07:33.420
So if we duplicate that, so control D and then just drag that down into here and we want this wheel

07:33.420 --> 07:42.540
here, drag that down into there, this one duplicate, bring that down to there and this one duplicate

07:42.540 --> 07:45.360
and bring that into here.

07:45.570 --> 07:46.050
Okay.

07:46.050 --> 07:54.000
So now that we've done that, we go back to our wheels that we've had before that have the mesh and

07:54.000 --> 07:55.440
the collider on it.

07:55.440 --> 07:57.900
And I'm going to shift select them.

07:59.640 --> 08:00.040
Oh, sorry.

08:00.060 --> 08:01.010
Control.

08:01.020 --> 08:04.440
Select them or command, select them and remove.

08:06.390 --> 08:09.000
The Wheel collider and the.

08:09.820 --> 08:11.200
Drive script from them.

08:11.200 --> 08:14.560
So they are just going to be the meshes.

08:14.770 --> 08:21.580
Then inside of these colliders, if you select them, you'll see that you've ended up with all the code.

08:21.580 --> 08:27.850
Obviously the drive needs to know about the actual mesh that we're wanting to affect.

08:27.850 --> 08:31.150
So this here needs to be aligned with that.

08:31.150 --> 08:32.320
We'll there.

08:32.560 --> 08:38.530
So let's go back into our drive script and then at the top here, we're going to feed that in.

08:38.530 --> 08:40.360
So we want a public.

08:41.680 --> 08:43.130
Game object.

08:44.110 --> 08:47.770
We'll mesh, which we will set.

08:47.770 --> 08:53.710
And then down here in our go, it's not this dot transform that's being affected.

08:53.710 --> 08:56.920
It's going to be the wheel mesh.

08:58.060 --> 09:03.280
So this will be we'll mesh like that.

09:03.280 --> 09:05.680
Okay, so save that script.

09:05.680 --> 09:08.200
Let's go back into the drive.

09:08.200 --> 09:16.630
Now we want to select the front left and we want to grab the front left mesh model here, this one and

09:16.630 --> 09:23.290
drag and drop that down into the we'll mesh for it and then we want our front right one do the same

09:23.290 --> 09:24.220
with that one.

09:25.450 --> 09:31.570
Do make sure you get the correct ones lined up with each other because then you'll have the wrong collider

09:31.570 --> 09:33.550
affecting the wrong actual mesh.

09:33.550 --> 09:35.230
And then the last one is the rear.

09:35.230 --> 09:35.610
Right.

09:35.620 --> 09:38.950
I'll bring that down into there.

09:38.950 --> 09:39.970
Okay, great.

09:39.970 --> 09:42.580
So now we're ready for another test drive.

09:42.580 --> 09:45.010
So press play.

09:45.940 --> 09:52.840
And now if you drive, you'll see that your wheels are actually moving.

09:52.840 --> 09:56.140
Now I'm seem to have an error that I haven't picked up something.

09:56.140 --> 10:04.180
So the wheel collider attached to wheel two which is the right left two.

10:04.810 --> 10:07.900
Now do make sure you double check your own settings in here.

10:07.900 --> 10:13.840
The colliders are the wheel parts that will have the drive script and the wheel collider on them.

10:13.840 --> 10:18.820
Also make sure there's only one copy of each of those on there because it might have been duplicated

10:19.720 --> 10:28.180
and also remove the mesh filter and the mesh render from each of those colliders and the other ones.

10:28.180 --> 10:33.940
The meshes make sure that they don't have any of the wheel colliders or the drive script on them, but

10:33.940 --> 10:40.390
you are assigning them to the wheel mesh exposed variable on the colliders.

10:40.780 --> 10:45.520
So the wheel mesh itself will look like this in the inspector.

10:45.520 --> 10:53.110
It has a mesh filter and a mesh renderer and then the Associated Collider with it, which this one here

10:53.110 --> 10:57.040
has no mesh filter, no mesh renderer.

10:57.040 --> 11:03.610
You can see I remove those and then it has the wheel collider and the drive script and the drive script

11:03.610 --> 11:06.730
is assigned with the mesh that it associates it.

11:06.730 --> 11:10.690
So this has been dragged and put into there.

11:11.800 --> 11:18.610
Now that we've made those changes to the car prefab you want to select the car and over in the inspector

11:18.610 --> 11:25.690
go to overrides and right down the bottom you'll find apply all which will save all those settings back

11:26.020 --> 11:36.400
so down there okay so now when we press play and there we go we have a fully functioning car, well,

11:36.400 --> 11:39.370
at least you can go forward and backwards.

11:39.540 --> 11:39.910
Right.

11:39.910 --> 11:46.240
So when we come back in the next lecture, we'll start looking at how we can turn the car.
