How To Make Healthbar Value Slowly Decrease?
How do I make a healthbar value slowly decrease?
What I have: public Slider Healthbar;
void OnTriggerEnter(Collider enemy)
{
Healthbar.value -= Random.Range(2, 5); // I need help here!
if (Healthbar.value <= 0) {
anim.Play ("Dead");
}
}
What exactly do you want to do? Do you want the GUI to show a s$$anonymous$$dily decreasing HP going towards the new value? In that case, create a coroutine which constantly Lerps the Healthbar.value to another variable, e.g. "targetHP" and then you change targetHP when subtracting health.
OR do you want to have a damage over time effect? If you want a DoT you should use OnTriggerStay and then use Time.deltaTime like Full$$anonymous$$e7alJacke7 said.
Yes, exactly what I want! How do I write the code for s$$anonymous$$dily decreasing HP going towards the new value?
Please add to my code:
public Slider Healthbar;
public Canvas GameOver$$anonymous$$enu;
public Canvas NewHSCanvas;
public Canvas HBCanvas;
public Canvas BlockPlaceCanvas;
public GameObject WallGO;
public GameObject Land$$anonymous$$eGO;
public GameObject Cannon;
public Slider PlyrDead;
private Animation anim;
int isDead = 0;
//public Slider PlyrHlthDamaging;
//public Slider PlayerIsDead;
//bool isAttacking = false;
//int plyrDamaged = 0;
//int plyrDamageSet = 0;
//bool isplyrDamaged = false;
void OnTriggerEnter(Collider enemy)
{
Healthbar.value -= Random.Range (1.25f, 2.5f); // Here is where I want the new code to s$$anonymous$$dily decrease!
if (Healthbar.value <= 0 && isDead == 0) {
PlyrDead.value -= 1;
anim.Play ("Dead");
isDead = 1;
NewHSCanvas.enabled = false;
StartCoroutine(WaitDeathGOScrn());
NewHSCanvas.enabled = false;
}
}
IEnumerator WaitDeathGOScrn () {
//NewHSCanvas.enabled = false;
//yield return new WaitForSeconds(0.05f);
//NewHSCanvas.enabled = false;
//yield return new WaitForSeconds(1.95f);
yield return new WaitForSeconds(2.0f);
BlockPlaceCanvas.enabled = false;
GameOver$$anonymous$$enu.enabled = true;
HBCanvas.enabled = false;
}
Answer by FullMe7alJacke7 · Oct 05, 2017 at 03:49 PM
You could do something like....
Healthbar.value -= Random.Range(2,5) * Time.DeltaTime;
I'm not 100% sure if that line will work for what you want, but I have a feeling you need to incorporate the unity timescale into your code to reach the desired effect.
DeltaTime doesn't need to be defined. It might not work exactly how I wrote it, but I'm sure there is a way to use it correctly that would help you.
You might want to try using a float to keep track of time and then subtract the amount of time passed from the healthbar value float. Another way would be to use a coroutine as they can be used in ways that would be similar to doing something every frame, except they don't rely on framespeed
Your answer
![](https://koobas.hobune.stream/wayback/20220612140306im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Getting average value of slider while playing. 0 Answers
Why Does Prefab Loose UI elements on Scene Change 0 Answers
How do I take out a UI Slider's value? 0 Answers
Is there a way to call on the highest value of an integer that has been set? 1 Answer
I am trying to change the value of a slider trough a script 1 Answer