- Home /
How to make WorldToScreenPoint work with an orthographic camera?
I'm trying to place a GUI.Label
over objects and I'm using an orthographic camera to look at the scene.
My code:
Vector3 screenPos = Camera.main.WorldToScreenPoint(position);
GUI.Label(new Rect(screenPos.x, screenPos.y, 100, 20), "Hello World!");
It seems that Camera.main.WorldToScreenPoint(position)
line returns a result that's not valid (it works only with perspective camera). The point on screen is correct on X axis (left-right) but Y axis (top-down) it's off.
How much it's off depends on target and camera world positions, but not on camera size (X on the screen is still correct after manipulating orthographic camera size).
What calculations needs to be done here to correctly place the label?
Answer by Namey5 · Jul 30, 2019 at 06:28 AM
Try inverting the y-axis of the returned screen coordinates, i.e.
GUI.Label(new Rect(screenPos.x, Screen.height - screenPos.y, 100, 20), "Hello World!");
In theory the function should work regardless of the camera's perspective setup because the projection matrix is only used to remap perspective coordinates into (essentially) orthographic coords. Sometimes, however, the y-axis may be flipped for various reasons, and looking at the image that seems to be the case here.
Your answer
Follow this Question
Related Questions
How to make WorldToScreenPoint work with an orthographic camera? 0 Answers
Orthographic camera size for 1080p quad @ 1920mx1080m 1 Answer
Center an object in Ortographic view 1 Answer
Orthographic camera cuts off half of screen in Web Player - resolution error? 1 Answer
Finding the right ortho size to fill the screen with an object 1 Answer