Racing Car Game # Day 2 — Rotate Wheel Mesh

Objective: Rotate the wheel mesh with the wheel collider

Jean-Noel Seneque
4 min readNov 18, 2021

In the previous article, Car Racing Game Series, I introduced the series and demonstrated how to drive the car back and forth using wheel colliders. In this article, we will look at how to sync the wheel mesh’s position and rotation with its wheel collider.

Where we left off if you look very closing, the wheel mesh doesn’t actually rotate (roll) when the vehicle is moving but if you select the wheels and press Play, in the Scene view you can see the wheel colliders rotating.

Wheel mesh does not rotate
Wheel colliders rotate

Let’s head back to the Locomotion script. First, we need to declare two variables in the Move method that will hold the rotation and position of the wheel collider. Next, get the position and rotation of the wheel collider by using the GetWorldPose function that writes back to the variables and applies those variables to the game object holding the mesh.

Press play and see how that looks now.

Hmmm…not what we were expecting but if you look back at your code, we are changing the position and rotation of the game object which has the wheel collider that is also rotating and changing position. We need to separate the logic and processing from the graphic, always (MVC).

I have gone ahead and renamed the game objects in the hierarchy and created a new prefab.

Next, go into the prefab, and let's create a new empty game object for each wheel. It is important you make it a child of the Wheel Mesh so when you move it out, it retains the same position values. Put the Mesh as a child and copy the Wheel Collider to the new game object. Remove the Wheel Collider that is on the Wheel Mesh. Do that for all four wheels. This separates the behaviour from the graphics.

You should end up with a parent game object that has the Wheel Collider and its child that has the mesh.

If you go to the Locomotion script on the Car, it has lost the references to the Wheel Colliders because we have moved them. On the Locomotion script component in the Inspector, change the number of Wheel Colliders to 0, lock the Inspector and select and drag each of the Wheel Colliders to the array.

Now that the wheel mesh is a child of the wheel collider, we can reference this in the code.

Save the script, head back into the Editor, and press play. We now have the wheel meshes moving and rotating with the wheel collider.

In the next article, we will add steering to the vehicle.

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 help or if there is something you think I can improve.

--

--

Jean-Noel Seneque

A Data & Analytics Consultant who is expanding into developing experiences in XR, Enterprise and Gaming space using Unity www.jeannoelseneque.com