# Unity Physics: Wheel Collider Basics

--

When it comes to making physics based vehicles in Unity, look no further than the wheel collider component!

Rover

I will be using this rover model from the Filebase asset library by GameDevHQ.

I played around with the hierarchy a few different ways, and they all worked great for me. I initially added the wheel collider directly to the wheel mesh game object with no issues.

You can also use empty game objects to hold the wheel collider, but make sure you position each object at the same position as it’s corresponding wheel mesh game object.

For the sake of this article and coding the wheel mesh so rotate with the wheel collider, I will use the below hierarchy where the wheel mesh object is a child of the wheel collider.

Wheel Collider

The wheel collider component has a lot of options that I won’t cover in this article, but feel free to explore them on your own. For now, let’s take a look at the suspension distance and radius.

Your wheel collider will most likely look offset. Do not try to fix this by changing the position of the collider! What you are seeing here is the suspension distance at work.

You can adjust the suspension distance to best reflect the vehicle you are making, be it an off-road truck or a race car.

Temporarily setting the suspension distance to zero will perfectly align your wheel collider visually.

You can now accurately use the radius value to find the perfect size for your wheel before reassigning your suspension distance.

Center of Mass

The center of mass has a massive impact on how your vehicle will handle. To see the center of mass on the parent game object that has the rigidbody, navigate to Window>Analysis>Physics Debugger.

Change the Draw Gizmos for option to Center of Mass.

You can see the center of mass is located in the center of the small body on top of those big wheels and suspension.

This is because on the rigidbody, Automatic Center of Mass is enabled by default.

Let’s see how the rover performs as my settings are right now.

When you disable automatic center of mass, another option appears to manually assign the position yourself.

I left the settings at zero, which places the center of mass down near the bottom of the wheels.

You can see the result is much more stability, and the ability to jump the wall that previously derailed it.

Scripting

This class is attached to the main parent object of the rover. Fixed Update is used to get the position and rotation of all four wheel colliders, and then assign that data to the wheel mesh objects.

For a simple and fun test, I let the user press the left mouse button to add torque to the rear wheels, and reset the torque on button release.

`public class Rover : MonoBehaviour{    private Rigidbody _rb;    [SerializeField] private WheelCollider _rearLeftWheel;    [SerializeField] private WheelCollider _rearRightWheel;    [SerializeField] private float _torque = 50f;    [SerializeField] private float _maxTorque = 1000;    private bool _isAccel = false;    [SerializeField] private List<WheelCollider> _wheelColliders;     // Start is called before the first frame update    void Start()    {        _rb = GetComponent<Rigidbody>();    }    private void Update()    {        if (Input.GetKeyDown(KeyCode.Mouse0))            _isAccel = true;        if (Input.GetKeyUp(KeyCode.Mouse0))        {            _isAccel = false;            _rearLeftWheel.motorTorque = 0f;            _rearRightWheel.motorTorque = 0f;        }    }    private void FixedUpdate()    {        if (_isAccel)            RearWheelDrive();        foreach (WheelCollider wc in _wheelColliders)        {            RotateWheels(wc);        }    }    private void RearWheelDrive()    {        if (_rearLeftWheel.motorTorque < _maxTorque)            _rearLeftWheel.motorTorque += _torque;        if(_rearRightWheel.motorTorque < _maxTorque)            _rearRightWheel.motorTorque += _torque;    }    private void RotateWheels(WheelCollider wc)    {        //assign rotation to correct wheel transform        Transform wheel = wc.transform.GetChild(0);        wc.GetWorldPose(out Vector3 pos, out Quaternion rot);        wheel.position = pos;        wheel.rotation = rot;    }}`

Here is the rover starting to drive off into the sunset. I may want to add some way to steer this thing!

I hope you enjoyed this introduction to wheel colliders in Unity. Happy trails and thanks for reading!

--

--

I am an artist and musician, that is currently diving headfirst into game development with C# and Unity3D.