I’m Trying to make a stamina bar for a survival game I’m working on. As far as I can tell my code works but I have one small issue that I feel will cause problems if not addressed now.
I have a scriptable obj for all the players stats so, stamina( being the max stamina) and staminaCurrent (for the current possible stamina that can be regenerated). and then the script that effects the stamina bar and how much is displayed and used at any time. there is a function that passively depletes the stamina and displays this (staminaCurrent) this all works fine the problem is that when I press LeftShit for the first time to run, if the current Stamina is less than the max, the stamina bar shoots up to the max stamina position then decreases from the max. now if I hold shift and let it deplete past the staminaCurrent declared in the scriptable obj then press shift the bar depletes from where it currently is and recharges to staminaCurrent like i want it too.
so my question is dose anyone know why it shoots up to the top the first time I press shift. thankyou in advance
also sorry I’m still somewhat new to coding and I’m sure there was a better way to approach what I’ve done so far but heres the code
public Slider staminaBar;
private int currentStamina;
public static StaminaBar instance;
private WaitForSeconds regenTick = new WaitForSeconds(0.1f);
private Coroutine regen;
public bool isDepleated = false;
public PlayerStatsSOBJ objStamina;
private void Awake()
{
instance = this;
}
void Start()
{
currentStamina = objStamina.staminaCurrent;
staminaBar.maxValue = objStamina.staminaCurrent;
staminaBar.value = objStamina.staminaCurrent;
InvokeRepeating("passiveStaminaDepleation", 1.0f, 1.0f);
}
public void Update()
{
if (currentStamina <= 0)
{
isDepleated = true;
Debug.Log("no Stamina");
}
if (currentStamina >= 1 && isDepleated == true)
{
isDepleated = false;
currentStamina += objStamina.staminaCurrent / objStamina.staminaCurrent;
staminaBar.value = currentStamina;
if (regen != null)
StopCoroutine(regen);
regen = StartCoroutine(RegenStamina());
Debug.Log("Stamina at 1");
}
}
public void UserStamina(int amount)
{
if (currentStamina - amount >= 0 && isDepleated == false)
{
currentStamina -= amount;
staminaBar.value = currentStamina;
if (regen != null)
StopCoroutine(regen);
regen = StartCoroutine(RegenStamina());
}
}
private IEnumerator RegenStamina()
{
yield return new WaitForSeconds(2);
while(currentStamina < objStamina.staminaCurrent && isDepleated == false)
{
currentStamina += objStamina.staminaCurrent / objStamina.staminaCurrent;
staminaBar.value = currentStamina;
yield return regenTick;
}
if (currentStamina <= 0)
{
Debug.Log("Stamina Depleated");
yield return new WaitForSeconds(2);
while (currentStamina < objStamina.staminaCurrent && isDepleated == true)
{
currentStamina += objStamina.staminaCurrent / objStamina.staminaCurrent;
staminaBar.value = currentStamina;
yield return regenTick;
}
}
regen = null;
}
private void passiveStaminaDepleation()
{
if (currentStamina > 10)
{
staminaBar.value -= 1;
objStamina.staminaCurrent -= 1;
}
}