How to Create a car controller in unity with C# script example

 Creating a car controller is a complex process it involves many concepts like physics, input, scripts and many more. In this example we are going to create a simple car controller based on collider and with single script. Lets take a look at step by step process.

  • Get a car prefab from assets store or create your own by using box as a body and cylinders as tires for this example.
  • Add Rigidbody component to car parent gameobject.
  • Now add a box collider to this car object and expand it so cover whole car as well as wheels. Also make sure there is no other collider attached with car, tires, or any other child object.
  • In this example we used wheels as child of car gameObject.
  • Now create a new C# script with following code and attach with car parent object.
  • Drag all wheels transform wheels field in inspector of this script for wheels transform referencing.

Code :
using UnityEngine;

public class CarController : MonoBehaviour
{
    public float speed = 10.0f;
    public float steeringAngle = 4.0f;
    public Transform[] wheels;
    public float wheelRotationSpeed = 100.0f;
    private float currentSpeed;

    void Update()
    {
        // Getting horizontal and vertical input
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        // Getting speed and changing position and rotation of car
        currentSpeed = verticalInput * speed;
        transform.position += transform.forward * currentSpeed * Time.deltaTime;
        transform.Rotate(Vector3.up, horizontalInput * steeringAngle);

        // Using brake when space key pressed
        if (Input.GetKey(KeyCode.Space))
        {
            currentSpeed = Mathf.Lerp(currentSpeed, 0, Time.deltaTime * 2);
        }
    
        // Rotating all wheels
        for (int i = 0; i < wheels.Length; i++)
        {
            wheels[i].Rotate(Vector3.right * currentSpeed * Time.deltaTime * wheelRotationSpeed);
        }
}

Now lets take a look how our script work

  • First we declare few variables for our car to work and control. We can control car speed by changing speed variable and other properties by changing its value.
  • We declare a wheels variable for refencing to wheels of car. so wheels of car rotate on moving.
  • We update all car moving and everything in Update() method. First we get horizontal and vertical input by Input.GetAxis() method.
  • then calculate car's current speed after that we change position of car according to current speed.
  • We also rotate car transform based on horizontal input.
  • We used Input.GetKey(KeyCode.Space) method for braking of car user press space key then we decrease car current speed.
  • In last we go through all car wheels transform and rotate then based on car speed. 

This is an example how we can create a simple car control. You can also edit it according to your need and can also create more advanced car controller. There are many ways of creating car controller. This is just a simple one.

Car controller in unity


Comments