health bar position formula

I need a formula to position my health bar object as it scales down.
i got close but i just couldn’t get it right.

i scale it down like so:

transform.localScale.x = (maxSize*HP)/maxHP;

Then on my next line i need to reposition it based on how much it just shrank. otherwise my healthbar would shrink from both ends, instead of shrinking to the left.

the closest iv gotten was with this formula:

transform.localPosition.x = noHP_X+(HP*(-noHP_X/maxHP));

noHP_X is the desired X location when health is 0.

and maxHP_X is the desired X location for when health is full. this variable isn’t used though… might be apart of the problem. it seems like it should be in there i just dont know where.

its all good except for a small gap between the start of the health bar and its background, as the health goes down the gap slowly closes until zero HP, at which point its finaly in the right spot.

if anyone could help me correct my formula, or just show me a better way to do it that would be sweet.

another way to do it is to use Image.Fill with a value from 0 to 1. and don’t need to reposition

Or use the slider from the 4.6 UI. Take a look at Unity Connect

You’re problem is that you’re not using a fullHP_X. As soon as you bring that into the equation it should work.

Get the initial x or length in units (during initialization or by hand will work). Then use:


The above calculates distance to the center of the health bar from the end.

double foo = X_AT_FULL_HP - X_AT_NO_HP;

But this one calculates the distance from the original position from the end position (since it will end up at the end of the bar).

//Then you just scale it the same way you scale the length.
transform.localPosition.x = X_AT_NO_HP + HP*foo;

The general formula behind this is scaling from a point which isn’t the origin. And you do that by scaling all the elements (as you already are). And then scaling all the relative positions too. Then the solution is elegant and simple. Just scale position by HP.

This formula should work for both GUI elements or GameObject elements. Put it all together:

// I seperated the HP ratio. Mathematically it makes more sense
// to scale by a ratio, rather than correct an arbitrary number.
transform.localScale.x = maxSize * (HP/maxHP);

// The above example as a one-liner.
// hpBarX could be set in Start()
// If it's not trivial to find hpBarLength, use the second example above instead.
transform.position.x = hpBarX + hpBarLength/2 * (HP/maxHP);

Alternatively, you can just move the health bar mesh so that it’s origin is at the end of the bar. Then when you scale it it scales about that point. Because then the relative position from the origin of the scale is (0,0[,0]) and you can ignore it. This would also be achieved by change the transformation anchor. IDK if ‘anchor’ or ‘origin’ is more applicable to Unity3D.

PS: I have found in the past that getting a pointer to the Transform component is a huge performance improvement. IDK if this is still true, I’ve been away from Unity for some years.

transform_ = transform; in Start(), or
better yet in the editor before
runtime. Then use transform_.position