Int seems to be multiplying when holding key
So I have a script for stamina and for health that are almost identical.. and the issue I'm having is when I use my stamina it isn't using my value at a continuous rate it keeps stacking which speeds up my stamina loss is there a way to fix this?
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class PlayerStamina : MonoBehaviour { public Slider staminaBar; public static PlayerStamina instance; private WaitForSeconds regenTick = new WaitForSeconds(0.1f); private Coroutine regen; public int currentStamina; public int maxStamina; private void Awake() { instance = this; } // Start is called before the first frame update void Start() { maxStamina = 100; currentStamina = maxStamina; staminaBar.maxValue = maxStamina; staminaBar.value = maxStamina; } public void UseStamina( int amount) { if(currentStamina - amount >= 0) { currentStamina -= amount; staminaBar.value = currentStamina; if(regen != null) { StopCoroutine(regen); } regen = StartCoroutine(RegenStamina()); } } public IEnumerator RegenStamina() { yield return new WaitForSeconds(2); while(currentStamina < maxStamina) { currentStamina += maxStamina / 100; staminaBar.value = currentStamina; yield return regenTick; } regen = null; } }
Your answer
