I want fix position GUI Texture of Health bar on head enemy always.
because i used camera.WorldToScreenPoint, gui move to camera then position of bar not to same.
Camera.WorldToScreenPoint returns pixell coordinates, but the Y axis is starting from the bottom-left screen edge. You need to convert is to “GUI” coordinate system - starting at top-left:
var target : Transform;
function Update () {
var screenPos:Vector3 = camera.WorldToScreenPoint (target.position);
var x = screenPos.x;
var y = Screen.height - screenPos.y;
print ("x=" + x + "; y=" + y);
}
You didn’t add any offset to your texture. If you want your texture to be above the 3D object, you need to subtract some fixed or dynamic value:
var target : Transform;
var xOffset: float;
var yOffset: float;
function Update () {
var screenPos:Vector3 = camera.WorldToScreenPoint (target.position);
var x = screenPos.x - xOffset; // x offset
var y = Screen.height - screenPos.y - yOffset; // y offset
print ("x=" + x + "; y=" + y);
}