Car Racing Game Series
Objective: Moving a car using wheel colliders
In this article, we will get a car moving back and forth using Wheel Colliders.
Setting up the environment
I am a huge fan of the Synty Studios product line and they have kindly made available for free in the Unity Asset Store the POLYGON Starter Pack (Kit) that we will import into the project.
Create a Plane, name it Ground and reset the position and change the Scale x: 10, Y: 1, and Z: 10
Let’s get fancy and drag on a Skydome and clouds from the Starter Kit. Drag into the Hierarchy the following prefabs with these settings
SM_SimpleSky_Dome_01
SM_Generic_CloudRing_01
Now let’s bring in the car for this demo, find the SM_PolygonCity_Veh_Car_Small_01 and reset its position. You should have a scene similar to this. I am using an even fancier grid material.
We are now going to add a Wheel Collider component to each of the wheels but before that, let's remove the Sphere Collider as this will interfere with what we are doing.
With each of the wheels still selected, Change the Wheel Collider settings as shown. These are the settings I found to work well. Please go ahead and experiment.
If you were to press Play now, you will see warnings that the Wheel Collider needs to be attached to a Rigidbody.
Let's add a Rigidbody to the car’s body.
If you press Play now, the car behaves very differently from what you expect, or does it? if you think about it, those wheels are under suspension and the car only weighs 1 kg. Let's change the Mass to 750 and press Play. We are getting somewhere.
Let us now move the car. Create a Locomotion script and type the following:
Back in the Editor, click on the car body, drag the Locomotion script onto it, lock the Inspector and drag all four wheels into the Wheel Colliders array.
Press Play and using the UP and Down or W and S keys, we now have a car driving by the wheels.
If you enjoyed reading this article give me a Clap, also if you would like to see more, “Follow” me, so you may be notified of future releases. You may also send me a message if you need any further help.