I have made a script allowing to go up the value of the player's stamina after 2 seconds. My code works perfectly without the WaitForSeconds, but suddenly the rise of the stamina is done instantly and that's not what I want. So I added the WaitForSeconds but nothing helps, it doesn't work.
if (Input.GetKey(KeyCode.LeftShift))
{
currentstamina -= 1f;
if(currentstamina <= 0)
{
currentstamina = 0;
runningSpeed = walkingSpeed;
}
}
else if (Input.GetKey(KeyCode.LeftShift) == false)
{
if (currentstamina < 20)
{
if (regen != null)
StopCoroutine(regen);
regen = StartCoroutine(StaminaBack());
}
}
IEnumerator StaminaBack()
{
yield return new WaitForSeconds(2f);
while(currentstamina < startingstamina)
{
currentstamina += startingstamina / 100;
yield return regenTick;
}
regen = null;
}
Thank you in advance for the help you will provide me.