Hey, thanks for dropping in. I have a problem where this coroutine just wont stop, and everytime i press the run button it stacks, so in game stamina is constantly dropping after you press shift once, and it stacks. The coroutine is rundrainstamina, and here’s the code:
private void OnEnable()
{
runAction.performed += _ => InitialRun();
runAction.canceled += _ => Walking();
shootAction.performed += _ => ShootGun();
}
private void OnDisable()
{
runAction.performed -= _ => Walking();
runAction.canceled -= _ => InitialRun();
shootAction.performed -= _ => ShootGun();
}
private void Walking()
{
isRunning = false;
currentSpeed = walkSpeed;
}
private void InitialRun()
{
if (currentStamina - initialRunCost >= 0 && isRunning == false)
{
UseStamina(initialRunCost);
currentSpeed = runSpeed;
isRunning = true;
Running();
}
}
private void Running()
{
if (currentStamina - runCost < 0 || !isRunning)
{
StopCoroutine(runStam);
currentSpeed = walkSpeed;
}
runStam = StartCoroutine(RunDrainStamina());
}
public void UseStamina(int staminaDrain)
{
if (currentStamina - staminaDrain >= 0)
{
currentStamina -= staminaDrain;
staminaBar.SetStamina(currentStamina);
if(regen != null)
{
StopCoroutine(regen);
}
regen = StartCoroutine(RegenStamina());
}
else
{
Debug.Log("Not enough stamina");
}
}
private IEnumerator RegenStamina()
{
yield return new WaitForSeconds(staminaRegenStart);
while(currentStamina < maxStamina)
{
currentStamina += maxStamina / 100;
staminaBar.SetStamina(currentStamina);
yield return staminaRegenTick;
}
regen = null;
}
private IEnumerator RunDrainStamina()
{
while (currentStamina - runCost >= 0)
{
UseStamina(runCost);
yield return new WaitForSeconds(runDrainStaminaTick);
}
}
thanks to anyone who responds