WorldToScreenPoint doesn't work on Y screen axis

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?

Help is very much appreciated.

Cheers,
Simon

“Seems off a little bit”? How so? And how much? Is it by a consistent amount?

Are you sure your axes are set up the way you expect, with positive directions in the direction you expect?

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.

The y axis is flipped on WorldToScreenPoint.

HEre is some code for putting a label on a target object…

(Making code…)

1 Like

Here it is!

221948–8046–$tag_109.zip (7.02 KB)

2 Likes

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.

Anyway thanks a lot

1 Like

Wow. Thank goodness… that simple Y issue was my problem!

1 Like

For anyone wondering what the solution was without downloading the package:

Vector2 position = Camera.main.WorldToScreenPoint(transform.position);
position.y = Screen.height - position.y;
3 Likes

Thank you, this solved my issue as well!

Please use the like button to show your appreciation. It’s much better than necroing a thread that is nearly 14 years old.

Thanks.