From Unify Community Wiki
Jump to: navigation, search

The CarController class serves as a guide to facilitate the implementation and usage of the WheelColliderSource classes.

The usage of these classes is intended to be identical to the usage of the WheelCollider classes so little to no work should be required to replace the Unity wheel colliders with the source wheel colliders. Bellow is a brief description of how to quickly set up a car.

To use this class, simply follow the instructions bellow:

  • 1. Create a GameObject which will serve as the Car's chassis.
  • 2. Add a CarController script to the chassis.
  • 2. Add a Rigidbody to the chasis and give it a mass of 1500 and enable gravity.
  • 3. Create 4 GameObject which will serve as the car's wheels.
  • 4. Set the wheel's as children to the chassis and position them where you would like the wheels to appear. The children will become the wheels and as such should hold the wheel geometry. Note that the position of the wheels will be position at which the suspension is at maximum compression.
  • 5. Then assign the 4 wheels to the CarController properties in the editor. Note that the wheels have a default radius of 0.5 units and a suspension length of 1 unit so make sure that everything is to scale or alternatively modify all the default values to match your scale.
  • 6. Drive the car!


 * CarController
 * This class was created by:
 * Nic Tolentino.
 * rotatingcube@gmail.com
 * I take no liability for it's use and is provided as is.
 * The classes are based off the original code developed by Unity Technologies.
 * You are free to use, modify or distribute these classes however you wish, 
 * I only ask that you make mention of their use in your project credits.
using UnityEngine;
using System.Collections;
public class CarController : MonoBehaviour
    public Transform FrontRight;
    public Transform FrontLeft;
    public Transform BackRight;
    public Transform BackLeft;
    private WheelColliderSource FrontRightWheel;
    private WheelColliderSource FrontLeftWheel;
    private WheelColliderSource BackRightWheel;
    private WheelColliderSource BackLeftWheel;
    public void Start()
        FrontRightWheel = FrontRight.gameObject.AddComponent<WheelColliderSource>();
        FrontLeftWheel = FrontLeft.gameObject.AddComponent<WheelColliderSource>();
        BackRightWheel = BackRight.gameObject.AddComponent<WheelColliderSource>();
        BackLeftWheel = BackLeft.gameObject.AddComponent<WheelColliderSource>();
    public void FixedUpdate()
        //Apply the accelerator pedal
        FrontRightWheel.MotorTorque = Input.GetAxis("Vertical") * 300.0f;
        FrontLeftWheel.MotorTorque = Input.GetAxis("Vertical") * 300.0f;
        //Turn the steering wheel
        FrontRightWheel.SteerAngle = Input.GetAxis("Horizontal") * 45;
        FrontLeftWheel.SteerAngle = Input.GetAxis("Horizontal") * 45;
        //Apply the hand brake
        if (Input.GetKey(KeyCode.Space))
            BackRightWheel.BrakeTorque = 200000.0f;
            BackLeftWheel.BrakeTorque = 200000.0f;
        else //Remove handbrake
            BackRightWheel.BrakeTorque = 0;
            BackLeftWheel.BrakeTorque = 0;
Personal tools