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:

```
double foo = LENGTH_OF_HEALTH_BAR_IN_UNITS / 2;
```

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