Basically I made a code to control my health, hunger, thirst & stamina bars & it is not working
What is supposed to happen is when I start the game I have 100% health, hunger, thirst & stamina & my hunger & thirst is supposed to decrease over time, my stamina is supposed to decrease when I am running and my hunger and thirst should decrease a little faster while running & when I stop running my stamina is supposed to increase.
All of these things should have an effect on the health after they reach a certain % and when health <= 0 it’s supposed to print a string in the counsel saying “you died”.
What is happening now is the bars are set at 100% when I start the game which they should be but for some reason the string “you died” print’s immediately when I start the game like my health is <= 0 but the bars are showing they are at 100% and health & hunger bars never decrease over time as they should & the stamina does not decrease while I am running and make my hunger & thirst decrease a little faster as it should
So my question to you is how can I fix this problem and get my bars working properly, don’t know if it is something in my code that is written wrong or if I am missing something in the code or what it is but maybe someone will know the answer and will be able to help.
Here is the code:
public float Health;
public float HealthOverTime;
public float Hunger;
public float HungerOverTime;
public float Thirst;
public float ThirstOverTime;
public float Stamina;
public float StaminaOverTime;
public Slider HealthBar;
public Slider HungerBar;
public Slider ThirstBar;
public Slider StaminaBar;
public float MinAmmount = 5f;
public float Sprintspeed = 5f;
Rigidbody MyBody;
// Start is called before the first frame update
private void Start()
{
MyBody = GetComponent<Rigidbody>();
HealthBar.maxValue = Health;
HungerBar.maxValue = Hunger;
ThirstBar.maxValue = Thirst;
StaminaBar.maxValue = Stamina;
updateUI();
}
// Update is called once per frame
private void Update()
{
CalculateValues();
}
private void CalculateValues()
{
Hunger -= HungerOverTime * Time.deltaTime;
Thirst -= ThirstOverTime * Time.deltaTime;
if (Hunger <= MinAmmount || Thirst <= MinAmmount)
{
Health -= HealthOverTime * Time.deltaTime;
Stamina -= HealthOverTime * Time.deltaTime;
}
if (MyBody.velocity.magnitude >= Sprintspeed && MyBody.velocity.y == 0)
{
Stamina -= StaminaOverTime * Time.deltaTime;
Hunger -= HungerOverTime * Time.deltaTime;
Thirst -= ThirstOverTime * Time.deltaTime;
}
else
{
Stamina += StaminaOverTime * Time.deltaTime;
}
if (Health <= 0)
{
print("YOU DIED");
}
updateUI();
}
private void updateUI()
{
Health = Mathf.Clamp(Health, 0, 100);
Hunger = Mathf.Clamp(Health, 0, 100);
Thirst = Mathf.Clamp(Health, 0, 100);
Stamina = Mathf.Clamp(Health, 0, 100);
HealthBar.value = Health;
HungerBar.value = Hunger;
ThirstBar.value = Thirst;
StaminaBar.value = Stamina;
}
public void TakeDamage(float amnt)
{
Health -= amnt;
updateUI();
}