Search Unity

Vehicle speed up / slow down

Discussion in 'Scripting' started by Grune, Nov 13, 2009.

  1. Grune

    Grune

    Joined:
    Nov 8, 2009
    Posts:
    136
    I made myself some script, which moves my Vehicle (riggid body) acros a surface. Just forward back turning.

    Code (csharp):
    1. var speed = 10;
    2. var turnSpeed = 30;
    3.  
    4.  
    5. function Update () {
    6.    
    7.     print ("current speed = " + speed);
    8.  
    9.     if (Input.GetKey ("up"))
    10.     {
    11.         transform.Translate(Vector3.forward * Time.deltaTime * speed);
    12.         //transform.Rotate(Time.deltaTime * -12, 0, 0, Space.World);
    13.         //transform.Rotate(Vector3.up * 50 * Time.deltaTime,Space.World);
    14.     }
    15.      
    16.      if (Input.GetKey ("down"))
    17.     {
    18.         transform.Translate(Vector3.forward * Time.deltaTime * speed * -1);
    19.         //transform.Rotate(Time.deltaTime * 12, 0, 0, Space.World);
    20.         //transform.Rotate(Vector3.down * 50 * Time.deltaTime,Space.World);
    21.     }
    22.  
    23.     if (Input.GetKey ("left"))
    24.     {
    25.         //transform.Rotate(0, Time.deltaTime * 16, 0);
    26.         transform.Rotate(0, Time.deltaTime * turnSpeed * -1, 0, Space.Self);
    27.         //transform.Rotate(Vector3.left * 50 * Time.deltaTime,Space.World);
    28.     }
    29.  
    30.     if (Input.GetKey ("right"))
    31.     {
    32.         transform.Rotate(0, Time.deltaTime * turnSpeed, 0, Space.Self);
    33.         //transform.Rotate(Vector3.right * 50 * Time.deltaTime,Space.World);
    34.     }
    35. }
    My Question: How could i make the speed slowly buld up as i hold the button. And fall off over 2 seconds a i release the button.
     
  2. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,124
    simply use the dynamic friction propetry of the physics material to slow down the movement of you rigidbody
     
  3. Grune

    Grune

    Joined:
    Nov 8, 2009
    Posts:
    136
    Yes Thank's ill try it. But it'S not really wat i am up to: A "real" engine builds up speed. I want it not start/stop at once.

    Is there a way to hold the Button -Increase a variable, release and the var sinks back to zero ?
     
  4. sebako

    sebako

    Joined:
    Jun 27, 2009
    Posts:
    301
    I think you need something like this:

    Code (csharp):
    1.  
    2.  
    3. // higher movespeed with w and up key
    4.             if(Input.GetKey("w") || Input.GetKey("up"))
    5.             {
    6.                 if(speed <= maxSpeed)
    7.                     speed += 1;
    8.                 else
    9.                     speed = maxSpeed;
    10.             }
    11.             // lower movespeed with s and down key
    12.             else if(Input.GetKey("s") || Input.GetKey("down"))
    13.             {
    14.                 if(speed >= minSpeed)
    15.                     speed -= 3;
    16.             }
    17.             // lower movespeed if no key is pressed
    18.             else if(!Input.GetKey("w") || !Input.GetKey("up"))
    19.             {
    20.                 if(speed >= 0)
    21.                     speed -= 2;
    22.             }
    23.  
    24.  
    25.  
    rgds

    zem
     
  5. Troy-Dawson

    Troy-Dawson

    Joined:
    Nov 2, 2009
    Posts:
    120
    Update() is for per-frame things, but the engine uses FixedUpdate() for physics stuff.

    Physics generally run better with a fixed update independent of the framerate. I run my scenes at 500hz physics currently.

    Unity also has the rigidbody component where you can apply forces and torques to you objects. This is a good API to learn since it's the best way to do this stuff.

    http://unity3d.com/support/documentation/Components/class-Rigidbody.html
     
  6. orindoomhammer

    orindoomhammer

    Joined:
    Oct 6, 2009
    Posts:
    113
    you should set up a max and min speed. where max speed could be 10 or 100. from this you simply would do this.

    Code (csharp):
    1. var Speed = 0;
    2. var minSpeed : float= 0;
    3. var maxSpeed : float = 10;
    4. var Accel = maxSpeed / .4; //change this to set up how much you want to speed up and down.
    5.  
    6.  
    7.  
    what this does is adds your accel to the speed by what you specify. check my math though im not very good at it. i basicly figured this out by playing with differnt settings.

    after you get this set up your speed should look somethig like this.

    Code (csharp):
    1.  
    2.  
    3.  
    4. if(Input.GetButton("Acellerate")){  
    5. Speed = Accel + Mathf.Clamp(Speed, minSpeed, maxSpeed - Accel);
    6. }
    7.  
    8. if (Input.GetButton("Decellerate")){
    9. Speed = -Accel + Mathf.Clamp(Speed, minSpeed +Accel, maxSpeed );}
    10.  
    11. if(("Acellerate")){
    12.    rigidbody.velocity = transform.forward * Time.deltaTime * Speed; //Apply velocity in Update()
    13.  
    14.  
    this should help you out i cut out some things and changed them from my original script to make more sence to you in your project. i would not sugest copy and pasteing it as i am probly useing differnt var's and differnt key names. but this should be what your looking for. its a start. its also rigid body dependent so your going to need a rigid body controler on your mesh.
    also you should be aware that this code doesnt really decellerate properly it stops you abruptly. i am working on this as i have yet to get it working properly. too busy doing what im good at... modeling.
    but i will be revisting this script soon.
     
  7. Grune

    Grune

    Joined:
    Nov 8, 2009
    Posts:
    136
    Thank you for the help.

    First I tested the script from zemog86
    this is my setup so you have a engine script i separated it from a navigation script for rotation

    Code (csharp):
    1. var speed = 0;
    2. var maxSpeed = 200;
    3. var minSpeed = -20;
    4.  
    5. function Update () {
    6.  
    7. print ("Actual Speed: " + speed);
    8.  
    9. // higher movespeed with w and up key
    10.          if(Input.GetKey("up"))
    11.          {
    12.             if(speed <= maxSpeed)
    13.                speed += 1;
    14.             else
    15.                speed = maxSpeed;
    16.          }
    17.          // lower movespeed with s and down key
    18.          else if(Input.GetKey("down"))
    19.          {
    20.             if(speed >= minSpeed)
    21.                speed -= 3;
    22.          }
    23.          // lower movespeed if no key is pressed
    24.          else if(!Input.GetKey("up"))
    25.          {
    26.             if(speed >= minSpeed)
    27.                speed -= 2;
    28.          }
    29.  
    30.  
    31.     transform.Translate(Vector3.forward * Time.deltaTime * speed /10);
    32.  
    33. }
    Works ok, only Problem is: if you stop the aceleration goes to -1 or -2, so you move a little backwards

    Next i tried your script orindoomhammer
    I gues i did something wrong, this is my setup
    Code (csharp):
    1. var Speed = 0;
    2. var minSpeed : float= 0;
    3. var maxSpeed : float = 100;
    4. var Accel = maxSpeed / .4;
    5. //change this to set up how much you want to speed up and down.
    6.  
    7.  
    8. function Update () {
    9.    
    10. print ("Actual Speed: " + Speed);
    11.    
    12.     if(Input.GetButton("Acellerate")){
    13.     Speed = Accel + Mathf.Clamp(Speed, minSpeed, maxSpeed - Accel);
    14.     }
    15.  
    16.     if (Input.GetButton("Decellerate")){
    17.     Speed = -Accel + Mathf.Clamp(Speed, minSpeed +Accel, maxSpeed );}
    18.  
    19.     if(("Acellerate")){
    20.     rigidbody.velocity = transform.forward * Time.deltaTime * Speed;
    21.     //Apply velocity in Update()
    22.     }
    23. }
    Because when you hit the Button it just jumps to maxspeed and keeps it up. It does not acellerate and not decellerate if you release the Button.

    Thanks a lot, lots of stuff to keep me um experimenting on :D
     
  8. sebako

    sebako

    Joined:
    Jun 27, 2009
    Posts:
    301
    change this:

    Code (csharp):
    1. var Accel = maxSpeed / .4;
    to this:

    Code (csharp):
    1. var Accel = 1.5;
    play around with the number until you find one that is fine for you.

    best regards

    zemog
     
  9. orindoomhammer

    orindoomhammer

    Joined:
    Oct 6, 2009
    Posts:
    113
    told you it would be buggy. its my first script. the reason i have it set to /4 on my script is so you can scale the speed of each ship just by setting a max speed. workd pretty slick on my main code. but still has some bugs to work out.
     
  10. Grune

    Grune

    Joined:
    Nov 8, 2009
    Posts:
    136
    :D Thanks i inserted an else condition:

    Code (csharp):
    1. var Speed = 0;
    2. var minSpeed : float= 0;
    3. var maxSpeed : float = 150;
    4. var Accel = 3;
    5. var decel = 0.1;
    6.  
    7. //var Accel = maxSpeed / .4;
    8. //change this to set up how much you want to speed up and down.
    9.  
    10.  
    11. function Update () {
    12.    
    13. print ("Actual Speed: " + Speed);
    14.    
    15.     if(Input.GetButton("Acellerate")){
    16.         Speed = Accel + Mathf.Clamp(Speed, minSpeed, maxSpeed - Accel);
    17.         }
    18.    
    19.     else {
    20.         Speed = -decel + Mathf.Clamp(Speed, minSpeed +Accel, maxSpeed );
    21.         }
    22.    
    23.     if (Input.GetButton("Decellerate")){
    24.     Speed = -Accel + Mathf.Clamp(Speed, minSpeed +Accel, maxSpeed );
    25.     }
    26.  
    27.     if(("Acellerate")){
    28.     rigidbody.velocity = transform.forward * Time.deltaTime * Speed;
    29.     //Apply velocity in Update()
    30.     }
    31. }
    now you can step on the gas. release and it will slow down and you even have a handbreak
     
  11. orindoomhammer

    orindoomhammer

    Joined:
    Oct 6, 2009
    Posts:
    113
    yah my code is more for space ships and stuff, but looks like its fixed for you.
     
  12. nemo1992

    nemo1992

    Joined:
    Nov 6, 2009
    Posts:
    13
    I experienced a problem of accelearting rigidbody as a bug, may appear interesting for you (note that I'm writing it on the fly, probaably won't work but get the main idea):
    Code (csharp):
    1.  
    2. var maxSpeed = 10.0;
    3. var accelerationForce = 1.0;
    4.  
    5. function Update ()
    6. {
    7.    if(Input.GetKey("Accelerate"){
    8.    {
    9.       if(rigidbody.velocity >= maxVelocity)
    10.          rigidbody.velocity = maxVelocity;
    11.       else
    12.          rigidbody.AddForce(accelerationForce);
    13.    }
    14. }
    15.  
     
    heintesnaar likes this.
  13. a170982

    a170982

    Joined:
    Apr 20, 2021
    Posts:
    2
    Got for turning back or reverse?