- Home /
WorldToScreenPoint, isn't getting it right?
Not sure what's going on - but for some reason I can't get WorldToScreenPoint
to get me the right on-screen coordinates.
public void OnHover(bool isOver)
{
var screen = Camera.main.WorldToScreenPoint(background.cachedTransform.position);
Debug.Log("Slot: " + screen);
Debug.Log("Mouse: " + Input.mousePosition);
}
See:
The background
variable is a UISprite
- with a TopLeft
pivot point, so I should get the right coords.
I also tried using the UI camera (which is orthographic) with NGUITools.FindCameraForLayer(background.gameObject.layer);
- Same results. I also tried localPosition
.
Also tried this, just to see if I get a correct x coord - no avail:
var bounds = NGUIMath.CalculateAbsoluteWidgetBounds(background.cachedTransform);
Debug.Log("Bounds: " + Camera.main.WorldToScreenPoint(bounds.min));
Note that my screen is 1333x768 - Sometimes, if I hover over a slot that's over to the right, I get numbers like 1444 for the x on-screen coords - which is higher than my screen's overall width.
Note also that it's only the width that's not getting calculated right - the height is fine. I tried screen.x - Screen.width
and Screen.width - screen.x
both didn't give me logical results (they shouldn't but I was just experimenting)
What am I missing here, why am I not getting the right coords?
Thanks.
EDIT: What I'm trying to do, is get the distance between the topleft corner of the slot, and the point where I click on the slot. So I figured I could do:
var screenPos = Camera.main.WorldToScreenPoint(background.transform.position);
var dist = Vector2(Input.mousePosition.x - screenPos.x, screenPos.y - Input.mousePosition.y);
This used to work in the past for some reason - now it's not. Could it be a NGUI-related issue?
there is a small glitch in this function where the camera takes is size ratio from the x coordinate.
the camera your using to display NGUI is camera.main yes? if not then you must use the correct camera. because the camera might be in a different position to the main camera. which will give the results you are having.
what i was talking about if you Convert between WorldToScreenPoint and ScreenTowWorldPoint you can get a strange offset on the x axis making everything out of whack, to fix it, try adding Camera.size by your screen quotient.
e.g I want an object at 0,0 on my screen, my camera is an orthgrapgic with a size of 100. my screen is 16:9 ratio when converting i end up with an x coordinate 170 units less than it should be becuase unity had subtracted the quotient.
"the camera your using to display NGUI is camera.main yes?" - No, when you create an NGUI UI it comes with its own camera. As I mentioned before I tried both the cameras - my main camera which has a perspective projection - and the NGUI camera which is orthographic, both gave the same results.
does your orthographic camera have normal rects? are they: X = 0, Y = 0, W = 1, H = 1 ?
Answer by ArkaneX · Nov 23, 2013 at 10:01 AM
I've no NGUI experience at all, but for me it looks like your main camera is positioned on the left of NGUI camera, or that it is rotated counterclockwise around y axis.
Could you post positions and rotations of both cameras, as well as background.cachedTransform.position
from your example?
Dang! Yes, it seems like if I change the position/rotation of the main camera, stuff goes weird! (why?) - But then I wanted to double check calculating the thing from NGUI's camera - (which I could have sworn I done with NGUITools.FindCameraForLayer) - it worked out well.
If you change position/rotation, then camera sees different parts of the scene. If you move it left, then things which were on the left just a moment before, can now be in the center or in the right, thus giving you different screen coordinates. The same about rotating.
This was not visible for you, because you observed NGUI camera results only. But the main camera was looking elsewhere.