Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

224
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda