- Home /
health bar goes down with time
I want to make a health bar,hunger bar and energy bar that goes down in like 2 hour with c# scripts, I watched a tutorials but it didn't help me.
Answer by MattG54321 · Jul 19, 2017 at 01:24 PM
First, I'd recommend watching a few tutorials on health bars:
Unity - Health HUD - a bit old but a good starting point
Google search for "unity health bar" - a plethora of tutorials on this topic covering several ways to do it
If you're having trouble, come back with specific questions. We'd be happy to help!
As for decreasing player health over time, I'd recommend something like this:
public float playerHealth;
public float decreasePerMinute;
void Update () {
// We divide decreasePerMinute by 60 to turn it into deacreasePerSecond.
// We then multiply it by Time.deltaTime, which is the time, in seconds,
// since Update was last called.
playerHealth -= Time.deltaTime * decreasePerMinute / 60f;
}
I watched a few tutorials but it all about when the player is getting attacked or being in danger, thanks.
do you mean that it'll decrease in $$anonymous$$ute ? because I tried it and I waited but nothing happened
decreasePer$$anonymous$$inute is how much health the player loses each $$anonymous$$ute. I divide it by 60 to get how much health the player loses each second. I do this becase Time.deltaTime is in seconds, so the health lost also needs to be in seconds.
The player's health decreases by a tiny bit every frame. Essentially, it's decreasing slowly, but constantly.
$$anonymous$$ake sure your playerHealth is hooked up to your GUI, or you won't see a change. Try Debug.Log(playerHealth) and see if you can see a change.
Look , the player health still decreasing in the Inspector and it gets in $$anonymous$$us but nothing happened .
[1]: /storage/temp/98358-unnamed-1.png
Sorry if I'm misunderstanding your problem when you say that nothing happened, but are you saying that the health is decreasing, but the health bar is not getting smaller? Because the code above is just the math involved in decreasing the number amount of health, but not the code that does anything with that health number yet. You'll need to use that number to scale the size of the health bar down as it get's smaller. If I misunderstood you, clarify that for me.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do I expose a health serialize script on a health bar ? 0 Answers
identifier expected 1 Answer