Enemy Health Scale

Hi, I was wondering if someone could guide me on the right path for my problem. I was able to set up an HP bar for my enemies, but their scale always stays the same even when they’re far away. Is there a way to make it so that the HP bar’s scale would be relative to the enemy it’s attached to and not the camera?

This is the script I have so far:

function Update () {

	myHB.transform.position = Camera.main.WorldToViewportPoint(transform.position);
	myHB.transform.position.x -=  0.05;
	myHB.transform.position.y +=  0.3;
	myHB.transform.localScale = Vector3.zero;

	var healthPercent: float = curHealth / maxHealth;

	healthBarWidth = healthPercent * 64;
	myHB.guiTexture.pixelInset = Rect(10,10,healthBarWidth,4);
}

Pretty sure there’s no way to do what you’re saying if you’re trying to do it with the GUI system - or if there is, it would be quite complex. What you need to do is make the health bar an object (i.e. in 3D space) attached to the actual enemy. In that way, you wouldn’t have to do anything special at all for it to appear the correct size at distance.

As geppetto said. The straight forward option would be to attach it to the object itself and possibly billboard it as well.