Working Wheels in Unity

Simon Pham
5 min readMay 1, 2024

--

In previous blog posts, we talked about different types of colliders including primitive, mesh, and terrain. In today’s blog post, let’s discuss wheel colliders.

What are wheel colliders and why do we need it?

In Unity, wheel colliders are used to simulate realistic wheel physics and interactions with the environment, such as friction, suspension, and contact with terrain. However, wheel colliders themselves don’t have a visual representation in the scene. To create a visual counterpart for a wheel collider, you typically use a separate GameObject with a visual representation (like a mesh) that is controlled based on the wheel collider’s properties.

How to create working wheels in Unity?

Scene set up

I’ve created a scene that has a rover with four wheels.

Now, we will try to make the wheels spin as the rover moves down the slope.

The Rover GameObject has a couple of child GameObjects including a base and four wheel container GameObjects.

Each wheel container consists of a base and an actual wheel that has a Mesh Renderer.

Wheel 1

Creating wheel colliders

For each wheel, I’m going to create a parent GameObject that will act as its collider.

Let’s add a Wheel Collider component to the Wheel COL 1 GameObject.

You might notice that the collider looks a bit off, so we’re going to adjust it.

First, let’s zero out the Suspension Distance and adjust the Radius property, which I think 0.21 is a good number so that the collider fits the actual wheel.

Next, I’m going to add a Rigidbody to the Rover GameObject, and if you notice, each of our wheel colliders has a Spring and Damper property that helps absorb impacts and stabilize the vehicle.

Therefore, we’d need to adjust the mass of our rover to match with the Spring and Damper force; I’m going to go with 1500, which is the average mass of a car in kilograms.

Let’s hit the Play button to see what’s going to happen.

As you can see, our rover’s movement was not smooth at all. In fact, it responded poorly to the terrain impacts and even rolled over a few times. To fix this, I’m going to increase the Suspension Distance property in each of the wheel colliders from 0 to 0.1, which will help provide a buffer that allows the wheels to adapt better to uneven terrain as the rover moves down the slope.

And here’s the result.

As you can see, the movement looks much better; however, if you pay closer attention to the actual wheels, you will notice that they are not spinning and only the colliders are spinning, so let’s address that issue.

Making the actual wheels spinning

The Wheel Collider component doesn’t allow us to change the rotation and position of actual wheels in the Inspector, so we’ll need to do that through code. I’m going to create a script called Wheels and attach it to the Rover GameObject. Inside the script, we’re going to need to get a reference to the list of wheel colliders.

using UnityEngine;

public class Wheels : MonoBehaviour
{
[SerializeField] private WheelCollider[] _wheelColliders;
}

Next, let’s assign the wheel colliders to the array in the Inspector.

The Wheel Collider component has a method called GetWorldPose that we can use to get the position and rotation values of the collider, and then we can assign those values to the actual wheels.

private void FixedUpdate()
{
foreach(var wheelCollider in _wheelColliders)
{
Transform wheel = wheelCollider.transform.GetChild(0);
Vector3 pos;
Quaternion rot;
wheelCollider.GetWorldPose(out pos, out rot);
wheel.position = pos;
wheel.rotation = rot;
}
}

Notes: For anything that is related to physics, it’s better to use FixedUpdate rather than Update for more consistent performance.

And here’s the final result:

--

--

No responses yet