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

Need inspiration on how to tackle "decreasing and increasing a variable overtime"

Discussion in 'Scripting' started by misterG420, May 11, 2020.

  1. misterG420

    misterG420

    Joined:
    Jan 23, 2020
    Posts:
    31
    Hi there!

    I can't wrap my head around this seemingly easy problem. I have a spaceship that can fly faster (thruster), if I press shift/fire3. It sould go only faster for a certain time, e.g. until the engines are depleted. After a few seconds, when you don't press shift/fire3, the engine is powering up again to 100% and you can shift-fast-move again.

    Now this is what I got so far (I am leaving most of the script away, cause it is a massive player movement script):

    Code (CSharp):
    1. public void ThrusterCooldownCalculation()
    2.     {
    3.      
    4.         lostThruster += ThrusterDecreaseRate * Time.deltaTime;
    5.  
    6.  
    7.         if (lostThruster < maxThrusterAmount)
    8.         {
    9.             shiftThrusterMultiplier = 2f;
    10.  
    11.             if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
    12.                 animator.SetTrigger("ActivateLeftThruster");
    13.  
    14.             else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
    15.                 animator.SetTrigger("ActivateRightThruster");
    16.         }
    17.         else if (lostThruster >= maxThrusterAmount)
    18.         {
    19.             //???;
    20.         }
    21.     }
    Now, this is my though-process: I know I can use the first line of code to increase the lostThruster rate over time. It works well. I also know that I need to start the time.deltaTime in that funciton as it only gets triggered after I press Shift/fire3 and this specific function gets called. The issue I am facing now is that I can't wrap my head around the reverse process: After the thruster is depleted (at a 100, e.g. the maxThruster amount) it should count back down as long as I don't press Shift/fire3 again. So it is a typical cooldown effect, but I can't make it work, since within that function, I still count up in the beginning of the code and I can't reverse the effect..

    It might be a simple block in my head, but what would you recommend?

    Thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,767
    Think of it as a "cooldown timer," or in this case a "boost thrust timer."

    Code (csharp):
    1. const float MaxThrusterTank = 2.0f; // seconds
    2. float ThrusterTank = MaxThrusterTank;
    When you are NOT thrusting, this value slowly counts up at the recharge rate:

    Code (csharp):
    1. // when not boost thrusting:
    2. ThrusterTank += Time.deltaTime;
    3. // check if we're full
    4. if (ThrusterTank >= MaxThrusterTank) ThrusterTank = MaxThrusterTank;
    NOW... when you are double thrusting, either by explicit control or by having triggered a "boost state" that is sticky:

    Code (csharp):
    1. // when boost thrusting
    2. if (ThrusterTank > 0)
    3. {
    4.   ThrusterTank -= Time.deltaTime;
    5.   if (ThrusterTank <= 0)
    6.   {
    7.     // you have exhausted your boost thrust
    8.     ThrusterTank = 0;
    9.   }
    10.   else
    11.   {
    12.     // here you may decide to do whatever it is that gives you extra speed
    13.   }
    14. }
    ALSO, you want to make sure the tank is at least a little bit full before allowing thrust, otherwise you can get this half-boost-thrust situation.
     
    PraetorBlue likes this.
  3. misterG420

    misterG420

    Joined:
    Jan 23, 2020
    Posts:
    31
    Hey, thanks!

    Over night I came up with a simpler approach: I still use the same variables. If the thruster is fully deleted, I start a coroutine that sets the depleted Thruster back to zero, after a delay of 5 seconds.

    Downside to that is that it resets back to zero without counting down slowly. So I think your approach is nicer and more elegant...maybe i'll try it out later tonight :)
     
    Kurt-Dekker likes this.