Hi, I am trying to attach a simple screen box with some text to a 3D object. For the X axis this works correctly but on the Y axis the transform seams to be off a little bit. Furthermore when I translate the object or the camera the drawn box translates in the opposite direction of which it should translate.
var posX = 0;
var posY = 0;
function OnGUI () {
GUI.Box(Rect(posX,posY,100,20),"some info");
}
function Update () {
var screenPos = Camera.main.WorldToScreenPoint (transform.position);
posX = screenPos.x;
posY = screenPos.y;
}
Does anyone else experience the same problem?
What am I doing wrong?
Why does it work on X but not on Y?
well after some experimenting with Debug.Log() it appears that WorldToScreenPoint() returns a lot more then just a X and Y coordinate. It seams to return 6 values of which the first value is the X coord. the second looks like a static value and the third value appears to be changing the most when I translate the object or the camera on the Y axis. However to yield this number I tried to use posY = screenPos.z; which gives a closer result but is still off by and offset of about 200 pixels. Also it doesn’t allign perfectly. Note z here should be the distance to the camera and not the height in pixels which I am looking for.
Accoording to the documenation WorldToScreenPoint() should return a Vector 3 were X and Y are width and height and Z is depth or distance from the point to the camera in world space units.
Anyway I find it quite weird, first I am gonna try a reboot of my system maybe that some pointer somewere is trying to acceess an invalid memory location or something like that. First I’ll do a restart and if the problem persists I will post here again.
Thank you Flynn.
This solves my problem with a simple Screen.height - screenPos.y;
maybe the documentation should say that the Y axis is flipped and this can be corrected by using the screen height minus the output of the y component.