WEBVTT

00:04.860 --> 00:09.990
Before you can start creating any racing game where you're going to be relying on physics, you need

00:09.990 --> 00:12.870
to understand what's underlying the system.

00:12.870 --> 00:21.390
And it's not only the overall physics system, but what really is and literally is driving it are the

00:21.390 --> 00:26.610
wheels of your karts and the physics that is on those wheels.

00:26.610 --> 00:31.930
And the way you set that up is absolutely fundamental to getting your game correct.

00:31.950 --> 00:38.790
Now, if you've had any experience using the wheel colliders and the vehicles set up in unity, you'll

00:38.790 --> 00:47.250
know that it's seemingly very unpredictable, especially if you don't know where to start or what exactly

00:47.250 --> 00:48.700
you're trying to achieve.

00:48.720 --> 00:55.470
So in this lecture, we're going to go right back to basics and look at just a single wheel before we

00:55.470 --> 01:00.080
start looking at the eye control code to drive a car around.

01:00.090 --> 01:05.640
We're going to, first of all, set up a car with the physics system from basics.

01:05.640 --> 01:12.480
So attach this lecture is a starter package, bring that in to unity and you'll see that I've opened

01:12.480 --> 01:19.800
it up here inside of the car I seen you'll find a truck tire model that I put in there.

01:19.800 --> 01:28.410
And also this car model, which you might recognize from an old unity project where it had car racing

01:28.410 --> 01:29.640
and quite like this model.

01:29.640 --> 01:30.960
So we're going to use that.

01:30.960 --> 01:33.990
But first of all, we're going to use the truck tire.

01:33.990 --> 01:36.630
So let's focus on that truck tire.

01:36.840 --> 01:43.350
The basis of the mechanics of the physics for a car is all driven by the wheels.

01:43.350 --> 01:50.040
Now, the most important thing, if you want to apply this technique to other models of cars that you

01:50.040 --> 01:56.010
have bought from the assets or you've made yourself, the wheels have to be obviously a separate object.

01:56.010 --> 02:00.750
They can't be sort of part of the car mesh because they have to move independently.

02:01.110 --> 02:07.800
The other thing that's important is the direction of your access system or your pivot point for your

02:07.800 --> 02:08.700
wheel model.

02:08.910 --> 02:15.270
So the forward direction of it must be the forward vector, which is your Z vector that way.

02:15.330 --> 02:20.850
And the outward facing vector has to be the X, so it has to go this way.

02:20.880 --> 02:29.620
Now, you could remodel this tire for the other side of the car so that the red axis is pointing out.

02:29.620 --> 02:36.540
But there's no need to do that if you duplicate it and just move it over like this and then just hit

02:36.960 --> 02:43.230
the arc to rotate it around and just put it there.

02:43.230 --> 02:48.660
So now when we select it and hit the W, we'll see that.

02:49.620 --> 02:55.110
We've actually got a forward direction and it's outward is that way.

02:55.250 --> 02:55.330
Okay.

02:55.350 --> 03:00.960
So that's most important so that you don't have four different tire models and everything is facing

03:00.960 --> 03:01.770
the right way.

03:02.340 --> 03:08.700
So if we take a look at the truck tyre, first of all, let me just delete that second one.

03:08.700 --> 03:10.440
I made this one here.

03:10.440 --> 03:12.870
It is a single mesh all by itself.

03:12.870 --> 03:14.850
And that is important, as I just said.

03:14.850 --> 03:18.810
But it has no children, things going on whatsoever.

03:18.960 --> 03:24.030
What we're going to do with it is make it into a physics object using a wheel collider and a rigid body.

03:24.030 --> 03:36.210
So we add in the inspector a rigid body, and then we also add a component for a wheel collider.

03:37.080 --> 03:47.970
Now, once you've done that, you're going to see over in the same view a green shape around it, which

03:47.970 --> 03:50.010
defines where that wheel is.

03:50.130 --> 03:55.230
Let's see if I can put myself into just wireframe mode.

03:55.560 --> 03:57.750
Actually, I might go shaded wireframe.

03:58.080 --> 03:59.440
That might help you see it a bit.

03:59.560 --> 04:01.050
A little bit better?

04:01.620 --> 04:02.700
No, not necessarily.

04:02.700 --> 04:05.880
Let's just go into wireframe instead.

04:06.000 --> 04:06.510
Okay.

04:06.510 --> 04:10.680
So if we select the wheel now, it's going to be a little hard for you to see on my screen.

04:10.710 --> 04:15.750
If you follow along, you'll be able to recognize this is the collider that's going around here.

04:15.780 --> 04:25.140
You've then got this spring value that the tire can move along and it basically has to align with the

04:25.140 --> 04:29.300
actual model of the tire which is around here.

04:29.310 --> 04:30.620
So what are we going to do?

04:30.630 --> 04:38.610
Let me go back into shaded is we're going to grab hold of that wheel collider and actually move it up

04:38.610 --> 04:40.110
in the Y direction.

04:40.830 --> 04:45.030
So might need to just bring it up a little bit.

04:45.030 --> 04:47.250
So I might have to type it in 0.5.

04:47.280 --> 04:51.990
It's going to be way too high .1.2.

04:51.990 --> 04:55.860
So 0.15 is about right, I think.

04:56.520 --> 04:57.540
Let's see if we can see it.

04:57.540 --> 04:57.780
Okay.

04:57.780 --> 05:03.780
So you can see it there around that outside part and it's got about the same distance all the way around

05:04.200 --> 05:04.900
there.

05:04.920 --> 05:05.280
Okay.

05:05.280 --> 05:12.330
Let's also while we're here, we're going to just zoom out a little bit and get our main camera and

05:12.330 --> 05:17.760
make sure that it's focused on that tire so we can see what it's going to do and react.

05:17.910 --> 05:18.350
Okay.

05:18.360 --> 05:23.970
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

05:23.970 --> 05:30.780
and lift it up in the air a little bit and then press play, you'll see the physics on it start to work.

05:31.110 --> 05:33.300
Okay, so, whoa, what happened to the tire?

05:33.300 --> 05:41.640
Let's just zoom back again and I'm just going to play it again because the tire will come back down

05:41.640 --> 05:42.030
again.

05:42.030 --> 05:47.400
But what happens is it's bouncing on the ground and then it's going up and there it is.

05:47.400 --> 05:52.680
There it's going to get up as far as it can with that bounce and it's going to come back down, hit

05:52.680 --> 05:53.430
the ground.

05:53.430 --> 05:55.530
And when it does, it'll have even more bounce.

05:55.530 --> 05:58.620
And you saw that zoom away really quickly just then.

05:58.620 --> 06:00.390
Why is this happening?

06:00.400 --> 06:07.680
Well, the tire physics is meant to be holding the load of a vehicle.

06:07.710 --> 06:08.210
Okay.

06:08.490 --> 06:13.890
Now the actual mass of that vehicle, if we have a look over in the inspector, you'll see that the

06:13.890 --> 06:14.940
rigid body has a mass.

06:14.940 --> 06:18.930
Now, this is the mass that you want to load onto your tire.

06:18.960 --> 06:21.870
This basically represents one kilogram.

06:21.870 --> 06:24.480
Cars do not weigh one kilogram.

06:24.780 --> 06:32.040
The wheel collider is working on a wheel mass of 20 kilograms, which is probably about right for your

06:32.040 --> 06:33.480
average car tire.

06:33.690 --> 06:41.130
But for the car itself, you probably want something more like 1000 kilograms in that mass, which will

06:41.130 --> 06:48.510
then give you better reactions to the physics because you're not reacting to like holding up a one kilo

06:48.510 --> 06:50.100
block on this tire when it bounces.

06:50.100 --> 06:56.490
So now if we press play and have a look, we will get a much more respectable tire.

06:56.490 --> 06:58.470
And you saw that there.

06:59.730 --> 07:05.430
Okay, let's zoom in on this truck tire again and I'm going to try this wire frame thing should be able

07:05.430 --> 07:06.600
to see it a little bit better.

07:06.600 --> 07:11.310
If we turn off the lighting, it doesn't seem to make much difference, but now it's aligned.

07:11.310 --> 07:14.280
It's actually a bit easier for you to see.

07:14.490 --> 07:23.760
Okay, so what we've got with this tire is basically this orange bit is your suspension string.

07:23.760 --> 07:31.500
Now this is the center of the wheel collider and what it's trying to do in the physics system is to

07:31.500 --> 07:36.990
get this center part of the tire to the desired location along this spring.

07:36.990 --> 07:40.560
Now, how do we set that desired position at the moment?

07:40.560 --> 07:43.170
We have this spring that springs in both directions.

07:43.170 --> 07:48.720
If you come over to your wheel collider and have a look at the suspension spring and the target position

07:48.720 --> 07:49.050
you've got.

07:49.400 --> 07:51.110
Point five.

07:51.650 --> 07:59.240
So what happens when this now drops is that it allows this part of the collision when this thing actually

07:59.240 --> 08:05.810
hits the ground to spring up along this spring and then sort of bounce back down again.

08:05.810 --> 08:11.900
And we can have a better look at that if I then move my main camera again, align with view.

08:12.680 --> 08:18.380
And what I'm going to do is find my gizmo setting, which is just here, make sure that's turned on,

08:18.620 --> 08:24.320
and then you can select your tire and you'll be able to see that actual spring.

08:24.620 --> 08:33.320
Now let's just move the camera out a little bit and what's going to be in the X direction there.

08:33.350 --> 08:33.610
Okay.

08:33.710 --> 08:34.730
And we'll select the tile.

08:34.730 --> 08:37.610
Let's have a look at how it reacts on that spring.

08:37.790 --> 08:43.730
So you'll watch that center line that actually now comes down and it bounces up to there that you might

08:43.730 --> 08:46.940
have seen and then stays within that spring.

08:47.090 --> 08:52.520
So what do you think's going to happen if we take that center line and actually put it at the top of

08:52.520 --> 08:53.060
the spring?

08:53.060 --> 09:01.250
So if we go over to our wheel collider and change that target position to one K, notice that the target

09:01.250 --> 09:08.540
position, which is the center of the collider, is now sitting very much at the extreme top of that

09:08.540 --> 09:09.230
spring.

09:09.230 --> 09:10.670
So what's going to happen?

09:10.670 --> 09:13.040
Let's press play now.

09:13.040 --> 09:17.720
It's going to come down and bang, just doesn't do anything.

09:17.720 --> 09:26.060
So what you have to imagine is now happening is that this whole collider is trying to move down, but

09:26.060 --> 09:30.380
when it hits the ground, it wants to bounce up, or at least that's its reaction.

09:30.380 --> 09:36.740
But it's already sitting at the top of the spring, so therefore it doesn't really have anywhere else

09:36.740 --> 09:37.820
to go.

09:38.210 --> 09:38.510
Okay.

09:38.510 --> 09:42.200
Now, one last thing that's quite important in this.

09:42.200 --> 09:49.790
And first of all, let's just set the target position back to 0.5 so that we have our correct tire.

09:50.000 --> 09:52.490
Is this position down here?

09:52.490 --> 09:52.650
Okay.

09:52.670 --> 10:01.550
So this little green sphere, so what that is, is the force applied position so that when we apply

10:01.550 --> 10:10.220
a rigid body force to our wheel, this is the point that will get affected and of course cause our wheel

10:10.220 --> 10:11.090
to turn around.

10:11.120 --> 10:12.220
How is that set?

10:12.230 --> 10:15.350
Well, you can see it's set perfectly on the outside here.

10:15.380 --> 10:22.520
If for some reason you wanted to apply the force down in here inside of the tire instead of along here,

10:22.520 --> 10:27.530
then you've just got to change this force applied position distance, which is at zero.

10:27.560 --> 10:35.030
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.

10:35.030 --> 10:39.920
But because of the way we're creating these tires and the fact that it's actually going to be a ground

10:39.920 --> 10:42.710
driving vehicle, we want it to stay down there.

10:42.710 --> 10:43.010
Right.

10:43.010 --> 10:49.100
So that's a real quick overview of the wheel collider physics set up.

10:49.130 --> 10:54.500
In the next video, we'll come back and actually start moving this around with some physics.
