Unity 3D - Wheels and Wheel Colliders

In your very first set of 3D Games Programming tutorials, you created a basic car that went round a basic racetrack. Now that you have some experience with Unity, you'll create a more sophisticated car. It will have proper steering where the wheels turn and go round. You'll also create a much better racetrack, on proper roads, and a nice terrain. It will all look something like this when you're done:

Unity 3D racing game showing car, terrain, minimap, and speedometer

Notice the minimap and speedometer. You'll add those too.

Let's make a start by building the car. Actually, we'll make the wheels first, get them to drive along by themselves, and then add a car body.

So, start a new Unity project. Call it anything you like. Now add a Plane to the default Scene. Make it nice and big by changing the Scale values in the Inspector. Change the X and Z to 100, but leave the Y on 1. Create a Material and add it to your Plane. (You should know how to do all this, by now. If not, see the earlier tutorials.)

Anyway, you should have a Plane something like this:

Now add an empty game object (Game Object > Create Empty). Rename this to CAR-ROOT. And reset the Transform values in the Inspector:

(Check your Plane, as well. If it's not on all zeros for the Position, reset it.)

Now change the Y value for Position to 0.72.

Everything to do with the car will be stored under this CAR-ROOT object. When we drive the car around, it's this CAR-ROOT object that will be propelled along, and any objects we add to ROOT will move with it.

To get that to work, we need to add a Rigidbody component to CAR-ROOT. So, with CAR-ROOT selected in the Hierarchy, click the Add Component button in the Inspector on the right. Type Rigid in the search box and then select the Rigidbody component from the list. Set the Mass of the Rigidbody to 1500:

The Mass is measured in Kilograms. This will be the weight of the car body as it gets velocity added to it. The higher your Mass the more torque it needs to get it moving. If you left it on the default of 1, the car would fly off into the stratosphere when you tried to move it.

We'll be adding a script and an audio source to this CAR-ROOT game object. For now, though, we need a collider on it. So, click the Add Component button again. This time, add a Box Collider. (If we don't add the box collider, the car will go into a tailspin when we try to drive it.) The Inspector for CAR-ROOT should look like this:

We'll need to tweak the values for the Box Collider later, when we add the car body. For now, though, let's add some Wheel Colliders.


Unity Wheel Colliders

When you move a car, it's actually the Wheel Colliders that you move when you write the code. Any visible tyre objects that you attach to the colliders will then move along with them.

Right-click on your CAR-ROOT object in the Hierarchy. From the menu that appears, select Create Empty:

Rename your empty game object to WHEELS. Right-click WHEELS and select Create Empty from the menu again. Rename this new empty game object to FrontLeft. Your Hierarchy should look like this:

So WHEELS is a Child of CAR-ROOT and FrontLeft is a Child of WHEELS.

Now select FrontLeft in the Hierarchy. In the Inspector on the right, click the Add Component button. In the Search box, type Wheel and select Wheel Collider from the list:

When you add the Wheel Collider, the Scene and the Hierarchy should look like this (double-click FrontLeft to zoom in):

Unity Scene view showing a wheel collider

As you can see, the Wheel Collider is just an empty green circle. We'll be adding a tyre as a child of this FrontLeft wheel collider soon. But look at the Inspector on the right. You should see this:

Setting for the wheel collider in the Unity Inspector

We need to change some settings for this collider. So, change the following values in the Transform section:


X: -1
Y: 0.084
Z: 1.625


X, Y, Z: 0.9

And change the Center Y to 0.14. The Inspector should look like this:

In the Hierarchy, make sure FrontLeft is still selected. Right-click and select Duplicate from the menu that appears (or just press CTRL + D on your keyboard). Rename the Duplicate to FrontRight. Now change the X Position value in the Inspector from -1 to 1. You should now have two wheels on your wagon:

Now select both FrontLeft and FrontRight in the Hierarchy (CTRL click to select both). Duplicate these two items. Rename to RearLeft and RearRight. Change the Position values in the Inspector to these for RearLeft:


X: -1
Y: 0.049
Z: -1.795

And these for RearRight:


X: 1
Y: 0.049
Z: -1.795

Your Hierarchy and Scene should look like this:

Unity scene showing four wheel colliders

Yay, four Wheels! Well, four Wheel Colliders. Let add some tyres now.


Adding Tyres to Wheel Colliders

You can use anything you like for the tyres, including primitive game object like cylinders. However, we'll download something from the Unity Asset Store.

So, click on Window from the menus at the top of Unity. From the Window menu, select Asset Store. When the Asset Store launches in your browser, go to this page:


This is a fantastic wheel pack by Perfect Games, with more wheels than you could possibly need. Download the pack and install it. In the Project area of Unity, you should (eventually) see a Wheels item:

Move inside of the Prefabs folder to see all the wheel folders. Move inside of the Rim_01 folder. Then select the Rim01_L_Mate tyre:

Project folder showing wheel tyre models

Drag and drop Rim01_L_Mate from the Prefabs folder onto FrontLeft in the Hierarchy:

The size of the tyre is too big, so change the Scale values in the Inspector to 0.5 for the X, Y and Z. You should have a nice tyre added to your Wheel Collider:

A tyre added to the wheel collider in Unity scene view

Now to do the same for FrontRight: drag and drop Rim01_L_Mate onto the FrontRight item in the Hierarchy. Change the Scale values in the Inspector to 0.5 for X, Y and Z. Except, it will be the wrong way round. So, change the Y Rotation to 180. You should now have two front tyres:

In exactly the same way, add tyres to your RearLeft and RearRight Wheel Colliders. You should then have four tyres. Rename your tyres in the Hierarchy to these:


And here's what you should have:

All four tyres added to the wheel colliders in Unity scene view

Quite remarkably, we can add some code and drive these tyres. The coding's a bit tricky, though, so you might want to hang onto your hat! (If you don't own a hat, you might want to go out and buy one, hang onto it, and then do the coding.)

<--Back to the Unity 3D Course Contents Page