Gamedev Hub

Raycast Vehicle Physics

Most beginners start with Unity’s WheelCollider, but they quickly find it “floaty” and hard to tune for arcade or high-performance racing. Raycast Vehicles are the industry standard for games like GTA, Mario Kart, and Trackmania.

1. How it Works

Instead of using physical wheel rigidbodies and joints, we use 4 Raycasts (or Sphere-casts) pointing down from the corners of the car’s body.

2. The Three Core Forces

1. Suspension (The Spring)

The suspension is a simple PD Controller (Proportional-Derivative).

2. Steering (Sideways Friction)

To make the car steer, we calculate how fast the wheel is moving sideways (relative to the wheel’s right-axis). sideVel = Dot(WheelRight, CarVelocityAtWheel) We then apply an opposite force to counteract that velocity. This is where you can implement Drifting by capping the maximum friction force.

3. Acceleration (Forward Force)

Engine torque is simply a force applied along the wheel’s forward axis: AccelForce = Input * EnginePower

3. Implementation (C# Snippet)

void UpdateWheel(Wheel w) {
    if (Physics.Raycast(w.pos, -transform.up, out RaycastHit hit, w.maxLength)) {
        Vector3 worldVel = rb.GetPointVelocity(w.pos);

        // 1. Suspension Force
        float offset = w.maxLength - hit.distance;
        float vel = Vector3.Dot(transform.up, worldVel);
        float force = (offset * spring) - (vel * damper);
        rb.AddForceAtPosition(transform.up * force, w.pos);

        // 2. Steering Force
        float sideVel = Vector3.Dot(w.right, worldVel);
        rb.AddForceAtPosition(-w.right * sideVel * friction, w.pos);

        // 3. Driving Force
        rb.AddForceAtPosition(w.forward * accelInput * enginePower, w.pos);
    }
}

4. Advanced: The Pacejka Magic

For realistic racing sims, the simple sideVel * friction isn’t enough. Professional games use the Pacejka Magic Formula, which calculates the “Slip Angle” of the tire. This allows for a realistic transition where the tire has maximum grip at a small slip angle and then “breaks” into a slide as the angle increases.

5. Why use Raycast Vehicles?