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


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?


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?

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?