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)]);
}