Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to slow down add torque rotation over time

Discussion in 'Scripting' started by sothern101, Jul 15, 2014.

  1. sothern101

    sothern101

    Joined:
    Jan 30, 2014
    Posts:
    28
    Hello, i'm starting to make a car game and have the moving and steering worked out. But when I let go of the acceleration key I want the car to gradually slow down.
    This is my current script:

    Code (JavaScript):
    1.  
    2. var wheelBL : WheelCollider;
    3. var wheelBR : WheelCollider;
    4. var wheelTL : WheelCollider;
    5. var wheelTR : WheelCollider;
    6.  
    7. var speed : float = 0;
    8. var maxSpeed : float = 10;
    9. var turning : float = 5;
    10.  
    11. function Update ()
    12. {
    13.     //Move forward
    14.     wheelBR.motorTorque = Input.GetAxis("Vertical") * speed;
    15.     wheelBL.motorTorque = Input.GetAxis("Vertical") * speed;
    16.    
    17.     wheelBR.brakeTorque = 0;
    18.     wheelBL.brakeTorque = 0;
    19.    
    20.     //Turn
    21.     wheelTL.steerAngle = Input.GetAxis("Horizontal") * turning;
    22.     wheelTR.steerAngle = Input.GetAxis("Horizontal") * turning;
    23.    
    24.     if(speed >= maxSpeed){
    25.         speed = maxSpeed;
    26.     }
    27.     if(speed <= 0){
    28.         speed = 0.0000;
    29.     }
    30.    
    31.     if(Input.GetKey(KeyCode.W)){
    32.         speed += 5.0 * Time.deltaTime;
    33.     }
    34.     if(!Input.GetKey(KeyCode.W)){
    35.         speed -= 1.0 * Time.deltaTime;
    36.     }
    37.    
    38. }
    But when I let go of W the speed variable does go down, but the car still goes at a constant speed.I understand that it may not be the script but the colliders, or it could be the script. If anyone here could help me gradually slow down the car when the acceleration key is not being pressed that would be amazing, thanks!
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    do you have any form of drag setup?
     
    Wesai and sothern101 like this.
  3. sothern101

    sothern101

    Joined:
    Jan 30, 2014
    Posts:
    28
    I feel dumb now for not looking at that. Thank you very much :)
     
  4. Patrickmol

    Patrickmol

    Joined:
    Nov 12, 2020
    Posts:
    6
    Here is my drag calculation I created by myself, I hope it will be helpfull:


    private Rigidbody thisRigid;
    private float startingDrag;

    private void Awake(){
    thisRigid = transform.GetComponent<Rigidbody>();
    startingDrag = thisRigid.drag;
    }

    private void FixedUpdate()
    {
    float rigidDrag = thisRigid.drag;
    if (engineInput != 0)
    rigidDrag -= (actualSpeed / accellerationTime);
    else
    {
    rigidDrag += 0.004f;
    Mathf.Clamp(actualSpeed, 0, 100);
    rigidDrag = Mathf.Clamp(rigidDrag, 0.001f, 5 / actualSpeed);
    }
    rigidDrag = Mathf.Clamp(rigidDrag, 0.001f, startingDrag);
    thisRigid.drag = rigidDrag;

    }
     
  5. Patrickmol

    Patrickmol

    Joined:
    Nov 12, 2020
    Posts:
    6
    lol I just saw this post was made 6 years ago
     
  6. sothern101

    sothern101

    Joined:
    Jan 30, 2014
    Posts:
    28
    Never too late. Thanks for the help :)