- Home /
ObjectLabel working inversely in y direction
I am using a variation of the ObjectLabel script to keep a GUI Button tracking a character around the screen. But while it almost works, the screen-y component is behaving strangely. As the character moves upwards on the screen, the button goes correspondingly downwards from the center of the screen, and vice versa. I am sure that it is a simple fix but cannot work it out. The screen x component works great.
var anims : String[] = ["jump","hop","dance"]; //animations var buttonTexture : Texture; //The texture to use on the button var offset = Vector3.up; //World space offset. 1 unit above object by default var clampToScreen = false; //Will the label be visible if object is off screen var clampBorderSize = .05; //screen space to leave when clamped var useMainCamera = true; //Use the camera tagged MainCamera var cameraToUse : Camera; //Camera to use if useMainCamera is false private var cam : Camera; //The camera we're using private var screenPos : Vector3; //The screen position of the button
function Start () {
if(useMainCamera) cam = Camera.main;
else cam = cameraToUse;
}
function Update () {
if(clampToScreen) {
var relativePosition = cam.transform.InverseTransformPoint(transform.position);
relativePosition.z = Mathf.Max(relativePosition.z, 1.0);
screenPos= cam.WorldToScreenPoint(cam.transform.TransformPoint(relativePosition + offset));
screenPos= Vector3(Mathf.Clamp(screenPos.x, clampBorderSize,Screen.width-(clampBorderSize+buttonTexture.width)),Mathf.Clamp(screenPos.y, clampBorderSize,Screen.height-(clampBorderSize+buttonTexture.height)),screenPos.z);
}
else screenPos= cam.WorldToScreenPoint(transform.position + offset);
}
function OnGUI() {
if(GUI.Button(Rect(screenPos.x,screenPos.y,buttonTexture.width,buttonTexture.height),buttonTexture))
animation.CrossFade(anims[Random.Range(0,anims.length)]);
}
Answer by Statement · Dec 09, 2010 at 04:07 PM
Rect(screenPos.x, screenPos.y,
buttonTexture.width, buttonTexture.height)
Try changing that to:
Rect(screenPos.x, Screen.height - screenPos.y,
buttonTexture.width, buttonTexture.height)
Because I guess your coordinates are just flipped.
Your answer
![](https://koobas.hobune.stream/wayback/20220613090402im_/https://answers.unity.com/themes/thub/images/avi.jpg)