To simulate accurate wheel behavior in Unity, you use a Wheel collider for each wheel. Wheel colliders manage wheel rotation and vehicle movement, and also have properties that simulate a suspension system.
The Wheel collider component is represented in the API as the WheelCollider
class.
The Wheel collider appears in the Scene view as a 2D circle (see Wheel collider visualization). However, it is actually a single Physics raycast. PhysX casts the ray down the local Y-axis along the direction of suspension through the center of the wheel. The start and end of the raycast are at the following points:
This raycast configuration means that a Wheel collider does not actually roll across other surfaces (such as the ground) while moving. In Play mode, the rotation of the Wheel collider does not change. However, the rotation of the wheel model does need to change so that the wheel appears to roll along the ground. To achieve this, the model and the collider need to be on two separate GameObjects. The Wheel colliders’ Transform should be fixed relative to the vehicle, and the models should be able to rotate. You can then configure the Wheel collider to send its global position to the wheel model and rotate the wheel model via script. For an example of this workflow, refer to the walkthrough Create an example vehicle with Wheel colliders.
For more details on the Wheel collider raycast setup in PhysX, refer to the PhysX 4.1 Vehicles SDK documentation.
An important implication of the raycast setup is that wheels don’t always smoothly roll up or step down variations in road level (for example, rolling up from the road onto a step). When encountering a step or curb, the wheel is likely to clip and then “pop” up only as the center line crosses the step:
The raycast can also slip down into small drops or steps in the road when the center point reaches them:
For this reason, the ground collision geometry must be as smooth as possible to ensure a smooth and accurate simulation. If you have bumps or dips in the ground, you must test and iterate on them to make sure you are happy with the wheels’ behavior.
When you select a Wheel collider, the Scene view displays a gizmo which provides a visualization of some key Wheel collider properties.
The gizmo’s visual indicators are as follows:
You can use the gizmo visualization for quick reference and debugging.