Search Unity

Decreasing a value when key is up.

Discussion in 'Scripting' started by hawx, Jul 27, 2013.

  1. hawx

    hawx

    Joined:
    Sep 29, 2012
    Posts:
    95
    Hi,

    I'm trying to decrease the speed of my bike , when the "W" key is up.

    Variables :
    Code (csharp):
    1. var Speed : float;
    2. var Acceleration : float = 0.1;
    3. var MaxSpeed : int = 13;
    4. var MinSpeed : int = -3;
    For the acceleration I have this :
    Code (csharp):
    1. if(Input.GetKey("w")  (Speed < MaxSpeed )){
    2. Speed = Speed + Acceleration;
    3.                         }
    So the bike is slowly speeding up , but I don't know how to make it slow down gradually when the "w" key isn't pressed,(Input.GetKeyUp("w")).

    Any help would be brilliant,

    Thanks
    Hawx
     
  2. Mister-E2

    Mister-E2

    Joined:
    Jun 6, 2013
    Posts:
    179
    Speed = Speed - Acceleration? :)

    Also, you should really be factoring delta time into movement. It is the best way to assure the same speed on all types of pcs (frame independent)
     
  3. hawx

    hawx

    Joined:
    Sep 29, 2012
    Posts:
    95
    I've tried that but GetKeyUp only returns once , so it only minus' 0.1 from speed. I've been looking for a different method.


    New Variable (for when the bike stops. :
    Code (csharp):
    1. var NeutralSpeed : int = 0;
    Here's the code for key up:
    Code (csharp):
    1. if(Input.GetKeyUp("w")  (Speed > NeutralSpeed )){
    2. Speed = Speed - Acceleration;
    3.  }

    Thanks ,
    Hawx
     
    Last edited: Jul 27, 2013
  4. wolvz38

    wolvz38

    Joined:
    Feb 6, 2013
    Posts:
    66
    Its because GetKeyUp is only called once and therefore your speed will only subtract the acceleration one time. You need to set a variable lets say a boolean to be true when the key is up to start the deceleration. then in your update you will check to see if deceleration is true and that your not already stopped and then you would continue with the speed - acceleration.. hope this makes sense :)
     
  5. Mister-E2

    Mister-E2

    Joined:
    Jun 6, 2013
    Posts:
    179
    Possibly something simple like this would be a good base to work from?

    Code (csharp):
    1.  
    2. if(Input.GetKey("w") )
    3. {
    4.  
    5. if(Speed < maxSpeed)
    6. Speed +=  Acceleration;
    7.  
    8.  }
    9. else
    10. {
    11. if(Speed > 0)
    12.  Speed -=  Acceleration;
    13. }
    14.  
    GetKey returns true while the player holds the key down. Therefore, when they stop holding it down it will be false and your speed can decrease :)

    GetKey
     
  6. hawx

    hawx

    Joined:
    Sep 29, 2012
    Posts:
    95
    Thanks guys! :) Both of these make sense, so I will try both of them. Thanks a lot!

    Hawx

    Note : Thanks for both of your inputs , I've ended up using what Mister-E2 put and have moved this:
    Code (csharp):
    1.  var transAmount = Speed * Time.deltaTime;
    2.  transform.Translate(0, 0, transAmount);
    To FixedUpdate , it now works perfectly , thank you both of you , I'm sure I'll use both methods in future.
     
    Last edited: Jul 28, 2013