Stamina not draining on key being held

I have this simple sprinting script and basically the problem is when I hold shift to sprint it goes down but not continuously. The only way to get it down to 0 is to spam the sprint key over and over again. If I hold it down he continues to sprint but my stamina regenerates back to full. Any idea why it won’t just continuously drain while holding sprint down?

             if (Input.GetKeyDown(KeyCode.LeftShift))
             {
    
                 if (Stamina >= StaminaDecreasePerFrame)
                 {
    
                     isSprinting = true;
                     speed = sprintSpeed;
    
                     Stamina = Mathf.Clamp(Stamina - StaminaDecreasePerFrame, 0.0f, MaxStamina);
                     StaminaRegenTimer = 0.0f;
    
                 }
    
             }
    
             else if (Stamina < MaxStamina)
    
             {
    
                 if (StaminaRegenTimer >= StaminaTimeToRegen)
                 {
    
                     Stamina = Mathf.Clamp(Stamina + StaminaIncreasePerFrame, 0.0f, MaxStamina);
    
    
                 }
    
                 StaminaRegenTimer += Time.deltaTime;
    
             }

GetKeyDown only tells you when the user starts pressing the key. If you want to know every frame that the key is down, use GetKey instead.

Also, “frames” aren’t guaranteed to be evenly spaced, so having variables like StaminaDecreasePerFrame doesn’t really make sense. You should either use FixedUpdate so that you get called on equal time intervals (instead of once per frame), or in Update you should use a PerSecond amount multiplied by Time.deltaTime (the amount of time that actually passed since the last frame).

1 Like

Buddy, thank you so much. I’m completely new to Unity and had no idea that that was even an option. MUCH APPRECIATED. Thank you for the advice, worked like a charm.