Make healthbar size from percentage instead of value.

Hello!

I got this code which determines the size of the health bars in my game.

float currentHealth;
float maxHealth;

	void OnChangeHealth (float Health)
	{
		healthBar.sizeDelta = new Vector2(Health, healthBar.sizeDelta.y);
	}

When my units get lots of hp the bar becomes ridiculously long. Can someone please tell me how to make the health bar size depend on percentage of remaining health instead?

Thanks in advance!

//Glimma

float currentHealth;
float maxHealth;
float maxSize;

     void Start(){
         maxSize = transform.localScale.x
     }

     void OnChangeHealth (float health)
     {
         healthBar.sizeDelta = new Vector2(health / maxHealth * maxSize, 
         healthBar.sizeDelta.y);
     }

Basically just make another variable for the standard size of the healthbar when the health is full :slight_smile:

If using an Image element then…HealthBarImage.fillAmount = currentHealth / maxHealth works the best.