'Controlling the Speed of the Car

I have been trying to control the speed of the car in Unity4. I am providing the code details below. Do I need to upgrade the code? As because when the brake(space bar) is pressed the Speed is set to Zero but when the brake is released the Speed is increasing again.

using UnityEngine;
using System.Collections;

public class CarMovementScript : MonoBehaviour
{


    public Vector3 com;
    public Rigidbody rb;
    public WheelCollider FrontLeft;
    public WheelCollider FrontRight;
    public WheelCollider RearRight;
    public WheelCollider RearLeft;
    public float maxspeed = 40;
    public float carspeed = 0;
    public float speed = 0.0f;
    float braking = 75.0f;
    float turning = 30.0f;

    void Start()
    {

        rb = GetComponent<Rigidbody>();
        rb.centerOfMass = new Vector3(rb.centerOfMass.x, -0.9f, rb.centerOfMass.z);
    }
    void Update()
    {
        if (speed <= maxspeed)
        {
            Debug.Log(speed);
            //this code makes car go forward
            RearRight.motorTorque = Input.GetAxis("Vertical") * speed;
            RearLeft.motorTorque = Input.GetAxis("Vertical") * speed;
            speed += 0.05f;
        }



        //this code works for braking of the car
        RearRight.brakeTorque = 0;
        RearLeft.brakeTorque = 0;

        //this code is for turning
        FrontRight.steerAngle = Input.GetAxis("Horizontal") * turning;
        FrontLeft.steerAngle = Input.GetAxis("Horizontal") * turning;


        //Breaking
        if (Input.GetKey(KeyCode.Space))
        {
            RearRight.brakeTorque = braking;
            RearLeft.brakeTorque = braking;
            speed = 0.0f;

        }
    }
}


Solution 1:[1]

Based on your statement:

when the brake is released the Speed is increasing again.

My guess is that your this piece of code needs attention:

    if (speed <= maxspeed)
    {
        Debug.Log(speed);
        //this code makes car go forward
        RearRight.motorTorque = Input.GetAxis("Vertical") * speed;
        RearLeft.motorTorque = Input.GetAxis("Vertical") * speed;
        speed += 0.05f;
    }

When you apply break, the speed goes to zero, but since speed <= maxspeed so it returns true and car starts moving due to speed += 0.05f. So you should probably block this condition unless there is an input for vertical axis. Like: (untested, to give you an idea)

    float vertical = Input.GetAxis("Vertical");
    if ((speed != 0 && speed <= maxspeed) || 
        (speed == 0.0 && vertical > 0.0))
    {
        Debug.Log(speed);
        //this code makes car go forward
        RearRight.motorTorque = vertical * speed;
        RearLeft.motorTorque = vertical * speed;
        speed += 0.05f;
    }

This way, if condition will execute iff either brake applied and from vertical axis there is a gain in speed or there is already a gain in speed and speed is limited with maxspeed

Hope it helps!

Solution 2:[2]

don't set speed to 0 instead decrement it by 0.05f.

remove this

if (speed <= maxspeed)
{
    Debug.Log(speed);
    //this code makes car go forward
    RearRight.motorTorque = Input.GetAxis("Vertical") * speed;
    RearLeft.motorTorque = Input.GetAxis("Vertical") * speed;
    speed += 0.05f;
}

and change

if (Input.GetKey(KeyCode.Space))
{
    RearRight.brakeTorque = braking;
    RearLeft.brakeTorque = braking;
    speed = 0.0f;

}

to

if (Input.GetKey(KeyCode.Space))
{
    RearRight.brakeTorque = braking;
    RearLeft.brakeTorque = braking;
    speed -= 0.05f;

}
else if (speed <= maxspeed)
{
   Debug.Log(speed);
   //this code makes car go forward
   RearRight.motorTorque = Input.GetAxis("Vertical") * speed;
   RearLeft.motorTorque = Input.GetAxis("Vertical") * speed;
   speed += 0.05f;
}

Solution 3:[3]

This is the code for turning. But the turning gets multiplied automatically at the beginning. What Code changes must be done to adjust the turning? FrontRight.steerAngle = Input.GetAxis("Horizontal") * turning; FrontLeft.steerAngle = Input.GetAxis("Horizontal") * turning;

Solution 4:[4]

paste this in update function/method.

void Update()
    {
        //20.0f is maxSpeed
        GetComponent<Rigidbody>().velocity = Vector3.ClampMagnitude(GetComponent<Rigidbody>().velocity, 20.0f);
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 NeverHopeless
Solution 2
Solution 3 Srijib Roy
Solution 4 Single-byte