Manual Car Driving Mechanism

This game is a simulation of real world driving, where you need precise control over Clutch, Accelerator and Brake to drive without any halt and increase car’s economy. Majority of the time on this project is spent in building the mechanics of the entire gearbox/clutch system. The project was done as a part of challenge at Unity Connect.

  • References for valid gear shift

  • Scripting

– Code includes huge junk of mechanism and trial and error.
Here is one part showing the gear ratio data and input values consideration.

void Update()
    {
        Slide_Acc(); Slide_Brake(); Slide_Clutch();
        h = Input.GetAxis("Horizontal");
        v = Input.GetAxis("Vertical");
        c = Input.GetAxis("Clutch");

        if (Input.GetKeyUp(KeyCode.RightArrow))
        {
            if (gear_index != 5 && c > 0.7f)
                gear_index++;
            else if (c < 0.7f)
                gear_index = 0;
        }
        else if (Input.GetKeyUp(KeyCode.LeftArrow))
        {
            if (gear_index != 0 && c > 0.7f)
                gear_index--;
            else if (c < 0.7f)
                gear_index = 0;
        }

        if (car_start)
        {
            _time += Time.deltaTime;
        }

        switch (gear_index)
        {
            case 0: GR = 1000000; break; //neutral
            case 1: GR = 3; break; //GR = 3:1
            case 2: GR = 2; break; //GR = 2:1
            case 3: GR = 1.5f; break; //GR = 1.5:1
            case 4: GR = 1; break; //GR = 1:1
            case 5: GR = 0.75f; break; //GR = .75:1
            case -1: GR = 3; break; //GR = 3:1 (with ideal gear) //reverse
        }

        if (Input.GetKeyUp(KeyCode.KeypadEnter))
        {
            car_start = true;
        }



        angle = maxAngle * h;
        if (car_start)
            engineRPM = 500 + Mathf.Abs(v) * 2500;
        else
            engineRPM = 0;


        clutch = c;
        actualRPM = engineRPM * (1 - clutch) * GR;


        car_speed = GetComponent<Rigidbody>().velocity.magnitude;
        foreach (WheelCollider wheel in wheels)
        {
            // a simple car where front wheels steer while rear ones drive
            if (wheel.transform.localPosition.z > 0)
                wheel.steerAngle = angle;

            if (wheel.transform.localPosition.z < 0)
            {
                if (v > 0.02f)
                {
                    if (wheel.brakeTorque != 0)
                    {
                        wheel.brakeTorque = 0;
                        accelerator = 0;
                    }

                    GearRPMTorque(wheel, true);
                    accelerator = v;
                }
                else if (v < -0.02f)
                {
                    if (wheel.motorTorque != 0)
                    {
                        wheel.motorTorque = 0;
                        brake = 0;
                    }

                    GearRPMTorque(wheel, false);
                    brake = -v;
                }
                else
                {
                    wheel.brakeTorque = 0;
                    GearRPMTorque(wheel, true);
                    accelerator = 0;
                    brake = 0;
                }

            }

            // update visual wheels if any
            if (wheelShape)
            {
                Quaternion q;
                Vector3 p;
                wheel.GetWorldPose(out p, out q);

                // assume that the only child of the wheelcollider is the wheel shape
                Transform shapeTransform = wheel.transform.GetChild(0);
                shapeTransform.position = p;
                shapeTransform.rotation = q;
            }

            late_z_posi = transform.localPosition.z;
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *