Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Draining Fuel while Button is Held

Discussion in 'Scripting' started by downhilldan, Feb 22, 2017.

  1. downhilldan

    downhilldan

    Joined:
    Nov 16, 2016
    Posts:
    90
    Hi All,

    I'm trying to get my player's fuel to drain at a constant rate when the up arrow is held and then return to the normal fuel drain rate when the up arrow is released. So the fuel is draining at X rate when the up arrow is not held and 2X when it is held.

    Here are my basic controls:
    Code (CSharp):
    1. if (Input.GetButtonDown ("Vertical") && yPosition < 5)
    2.         {
    3.             yPosition += 5;
    4.         }
    5.         if (Input.GetButtonUp ("Vertical") && yPosition >= 5)
    6.         {
    7.             yPosition -= 5;
    8.         }
    I'm very new to programming and threw this together to try to accomplish what I want here. Its close, but the longer you hold the up arrow, the faster the fuel drains:
    Code (CSharp):
    1. public bool test = false;
    2.  
    3. if (Input.GetButtonDown ("Vertical") && yPosition < 5)
    4.         {
    5.             yPosition += 5;
    6.             test = true;
    7.         }
    8.         if (Input.GetButtonUp ("Vertical") && yPosition >= 5)
    9.         {
    10.             yPosition -= 5;
    11.             test = false;
    12.         }
    13.  
    14. void Update()
    15.     {
    16.         StartCoroutine (testCoroutine ())
    17.     }
    18.  
    19. IEnumerator testCorutine()
    20.     {
    21.         while (test == true)
    22.         {
    23.             fuel = Mathf.MoveTowards (fuel, 0, Time.fixedDeltaTime * 0.1f);
    24.             yield return null;
    25.         }
    26.     }
    Thanks in advance!! :)
     
  2. kubajs

    kubajs

    Joined:
    Apr 18, 2015
    Posts:
    58
    You can use just one variable and one constant for this, let say fuelDrainSpeed and set this variable based on what arrow is pressed.

    In update method

    FuelDrainSpeed = normalFuelDrainSpeed
    If up arrow pressed then
    FuelDrainSpeed = 2 * normalFuelDrainSpeed

    Then you can just do anything with the speed.
    E.g. fuelAmount -= FuelDrainSpeed
    I am writing from mobile so no code but it is straighforward.
     
    downhilldan likes this.
  3. Mordus

    Mordus

    Joined:
    Jun 18, 2015
    Posts:
    174
    You're starting a coroutine in the update function. So every single frame you're starting up a new copy of the co routine.

    When the buttons not held coroutines simply end immediately since test = false. When the button is held the coroutines keep running in the loop draining fuel. Except instead of just having 1 coroutine running draining fuel you're adding more and more coroutines every frame, thus draining fuel faster and faster and faster until you let go of the button and all the coroutines end as test = false again.
     
    downhilldan likes this.
  4. downhilldan

    downhilldan

    Joined:
    Nov 16, 2016
    Posts:
    90
    Got it working, thank you!
     
  5. downhilldan

    downhilldan

    Joined:
    Nov 16, 2016
    Posts:
    90
    Ahhhh, that makes sense. Thanks for the info!