Prototyping with the Wheel Collider and Suspension Setup

After watching the 2018 Netflix release of “Lost in Space” and seeing the new design for the Chariot, I got motivated to check out the Wheel Colliders again which significantly simplifies vehicle movement controls in a Unity Scene.

Chariot-View

The Unity site tutorial presents the direct setup for using the wheel colliders and moving the vehicle, but it does cover how to handle the mechanics for the suspension system which is the component I find most interesting in car games especially when it has realistic spring, piston and joint actions.

Chariot-View1

I like the Chariot design but looking at the rear wheel setup, the suspension design seem to be closer to a motorcycle than a car which means the suspension rotate about a pivot instead of moving vertically — and vertical tracking just happens to be how wheel colliders work — the colliders track the ground collision and updates its y-position simulating the vehicle suspension.

Chariot-ViewR

In this example, I’ll match the vertical movement of the wheels as it moves through terrain.

Chariot-RearSuspension

I created a simple block model to represent the Chariot in Autodesk Maya with an approximation of the rear suspension system.

Chariot-Joints

To be closer to real-world mechanics, I configured a piston setup to match the wheel movement and visualize the behavior using joints setup with aim constraints.

Setup Considerations in Unity

While the wheel collider gets us operational quickly, we have the following considerations with the Maya 3D model and similar concerns with other 3D software modelling tools.  In this post, I use Autodesk Maya 2018.2 and Unity 2017.3.1f1.

I have Maya and Unity installed on the same workstation so I can work with Maya Binary (.mb) files directly during prototyping without having to do FBX game exports.  I can edit the 3D model in Maya and the changes are reflected in the Unity.

  • I use centimeter as my linear working unit in Maya — this allows me to get a 3D model in Unity at Scale Factor and File Scale = 1
  • I setup 60 fps for the animation in Maya across all my projects
  • Maya and Unity have different 3D axis setups (Right Handed vs. Left Handed rule coordinate systems), to simplify the effort just remember positive-Z is the forward axis for both platforms and the X-axis is negative values for each other
  • IK (Inverse Kinematics) Solver and Constraints in Maya are not imported to Unity
  • Joints (aka Bones) “forward” is the X-axis in Maya and joint chains follow the X-axis, something to note when writing a script to modify joint transform in Unity

The vehicle movement controls is based on the Unity tutorial with quick hacks to also update the suspension piston positions along with the visual wheel objects.

// Update Wheel GameObject based on Wheel Collider world transform
visualTransform.transform.position = position;
visualTransform.transform.rotation = rotation;

// HACK:  Update the rear suspension pistons
if (!steering)
{
	var offset = (index == 0) ? 0.5f : -0.5f;   // Position offset
	Joints[index + 1].position =                // from Wheel Collider Position
		position +
		offset * Joints[index + 1].forward;     // Joint forward is X-axis

	var dir = Joints[index].position -
			  Joints[index+1].position;

	// Apply "aim constraint" on joint transforms using LookRotation()
	Quaternion lookRotX0 =
		Quaternion.LookRotation(dir, transform.up) *
		Quaternion.Euler(new Vector3(0, 90, 0));    // Rotation Correction
	Joints[index + 1].rotation = lookRotX0;

	var dir1 = Joints[index + 1].position -
			   Joints[index].position;
	Quaternion lookRotX1 =
		Quaternion.LookRotation(dir1, transform.up) *
		Quaternion.Euler(new Vector3(0, 90, 0));    // Rotation Correction
	Joints[index].rotation = lookRotX1;
}

An interactive WebGL demo can be viewed at

http://orbitalfoundry.com/WebGL/Chariot1/index.html

WASD Keys control the vehicle
W/S – Move Forward / Backward
A/D – Turn Left / Right
Spacebar – Apply Brakes

Arrow Keys control the chase camera
Up/Down – control camera pitch
Left/Right – control camera yaw to vehicle
[” “]” (Brackets) – zoom in/out

Chariot-WebGL

For questions, comments or contact – follow/message me on Twitter @rlozada