Home > Net >  Unity Slider, health bars
Unity Slider, health bars

Time:05-26

I'm trying to make a health bar for a boss, I have a UI slider, I have a health variable and have the slider position set to that, but the variable is at full (above 1) and the slider shows full, great, but the problem is that when I go down from full the slider doesn't go down, Only when it is below 1.

Here is code I have:

public class SliderHealthBar : MonoBehaviour
{
    public Slider slide;

   [Range(0.0f, 1000)]
   public float health; // This is the health variable that exceeds 1, the max is at 1000
   public Gradient healthGrad; // This changes the length of the slider
   public Image img; // This changes the colour of the slider

   void Update()
   }
       img.color = healthGrad.Evaluate((float)health); // setting the color to health point on the gradient
       slide.value = health; // setting the slider value to health
   }

Is there something wrong here?

Thanks!

CodePudding user response:

I figured out that i would divide the public float Health by the max health that the boss had, this would make it go between 0 and 1.

500 (health)/ 500 (max health) = 1

And then it would be:

200 (health)/ 500 (max health) = 0.4

Hope this helps anyone else.

:)

CodePudding user response:

The solution to the question is simple. Divide health by a thousand.

img.color = healthGrad.Evaluate(health/1000f); 
  • Related