- Home /
Need Help With My Health,Hunger,Thirst & Stamina Bars. How Can I Get Them Working?
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();
}
change the print you died with print health and make tell us the health value, whats the health value in the inspector?
All The Values Are Set To 100 In the Inspector
whats the print showing in the console if you change the you died text for Health?
Without digging into the code, I see several problems
if (Hunger <= $$anonymous$$inAmmount || Thirst <= $$anonymous$$inAmmount)
{
Health -= HealthOverTime * Time.deltaTime;
Sta$$anonymous$$a -= HealthOverTime * Time.deltaTime;// Sta$$anonymous$$a -= Sta$$anonymous$$aOverTime * Time.deltaTime;
}
AND
Health = $$anonymous$$athf.Clamp(Health, 0, 100);
Hunger = $$anonymous$$athf.Clamp(Health, 0, 100); // Hunger = $$anonymous$$athf.Clamp(Hunger, 0, 100);
Thirst = $$anonymous$$athf.Clamp(Health, 0, 100); // Thirst = $$anonymous$$athf.Clamp(Thirst, 0, 100);
Sta$$anonymous$$a = $$anonymous$$athf.Clamp(Health, 0, 100); // Sta$$anonymous$$a = $$anonymous$$athf.Clamp(Sta$$anonymous$$a, 0, 100);
About your sta$$anonymous$$a, I would have done this:
if ($$anonymous$$yBody.velocity.magnitude >= Sprintspeed && $$anonymous$$yBody.velocity.y == 0)
// Using sqr$$anonymous$$agnitude is better for performances
if ($$anonymous$$yBody.velocity.sqr$$anonymous$$agnitude >= Sprintspeed * Sprintspeed && $$anonymous$$athf.Abs( $$anonymous$$yBody.velocity.y ) <= 0.01f ) // Comparing two floats is not advised due to the lack of precision
Also, make sure Sprintspeed
does not have a too big value. Use Debug.Log( $$anonymous$$yBody.velocity.magnitude + " - " + Sprintspeed);
to check the values
@HaGGGames before ask a new question about the same topic, check this answer, maybe your problem is the float comparision
Answer by kaarloew · Feb 13, 2019 at 06:55 AM
Use debugger to figure out what the actual values are during runtime.
https://unity3d.com/learn/tutorials/topics/scripting/debugging-unity-games-visual-studio