Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
How to make my stamina regenerate slowly?

I need some help on my mobile open world game project. I have a player who can walk and runs when we press a button. I made a stamina wheel (like in zelda botw), and when my player run the stamina decreases. I have also put a regeneration coroutine that make the stamina regen. But because my maxStamina = 1, the time between it is empty and full is really fast. Here is my code :

public static StaminaUI instance;
private WaitForSeconds regenTick = new WaitForSeconds(0.1f);
private Coroutine regen;

private void Awake()
{
    instance = this;
}

void Start()
{
    fillAmount = maxFill;      
}

void Update()
{        
     if (Ybot.MoveSpeed > 5f)
     {
        UseStamina(0.015f);
     }  
     
     if (fillAmount < 0.01f)
     {
        Ybot.MoveSpeed = 0f;
        Ybot.animator.SetBool("isRunning", false);
        Ybot.animator.SetBool("isWalking", false);
    }

}

public void UseStamina(float amount)
{
    if (fillAmount - amount >= 0)
    {
        fillAmount -= amount;            

        if (regen != null)
        {
            StopCoroutine(regen);
        }

        regen = StartCoroutine(RegenStamina());
    }
    else
    {
        Debug.Log("Not enough stamina");
    }
}

private IEnumerator RegenStamina()
{
    yield return new WaitForSeconds(1);

    while (fillAmount < maxFill)
    {
        fillAmount += maxFill/1;         
        yield return regenTick;           
    }
    regen = null;

}

Hope someone can help me to this little problem.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Since you increment every 0.1 seconds I think it should be

fillAmount += maxFill / desiredDuration * 0.1f;

where desiredDuration is the time in seconds needed to completely fill the stamina if it is 0.


Or as an alternative for smooth Updates instead of the 0.1 second steps you could do

while (fillAmount < maxFill)
{
    fillAmount += maxFill / desiredDuration * Time.deltaTime;      
    yield return null;           
}
over 4 years ago · Santiago Trujillo Report

0

You need to use Time.time and then do the regen if the TIme.time is > than a fixed interval. You have to realise that Update is called once per frame. This could mean it can be called anywhere between 30-90 times a second depending on your device's refresh rate.

Update is called once per frame so in your code you have to do something similar to this:

lastRegenTime and regenIntervals can both be floats (which can be expensive), but in Update the logic would be ->

            if (Time.time - lastRegenTime > regenInterval)
            {
                Regenerate();
            }

Set regenInterval to something low, and then set the incrementor for life at life+=0.02f; You can even do it more incrementally, or *=0.00002f;

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!