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

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):

public void ThrusterCooldownCalculation()
    {
      
        lostThruster += ThrusterDecreaseRate * Time.deltaTime;


        if (lostThruster < maxThrusterAmount)
        {
            shiftThrusterMultiplier = 2f;

            if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
                animator.SetTrigger("ActivateLeftThruster");

            else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
                animator.SetTrigger("ActivateRightThruster");
        }
        else if (lostThruster >= maxThrusterAmount)
        {
            //???;
        }
    }

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!

Think of it as a “cooldown timer,” or in this case a “boost thrust timer.”

const float MaxThrusterTank = 2.0f; // seconds
float ThrusterTank = MaxThrusterTank;

When you are NOT thrusting, this value slowly counts up at the recharge rate:

// when not boost thrusting:
ThrusterTank += Time.deltaTime;
// check if we're full
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:

// when boost thrusting
if (ThrusterTank > 0)
{
  ThrusterTank -= Time.deltaTime;
  if (ThrusterTank <= 0)
  {
    // you have exhausted your boost thrust
    ThrusterTank = 0;
  }
  else
  {
    // here you may decide to do whatever it is that gives you extra speed
  }
}

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.

1 Like

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 :slight_smile:

1 Like