Input.mousePosition and GUI.Window Issue

Hello,

I’m having trouble making a GUI.Window appear where I clicked on the screen.

Here is what I’m doing right now:

Player clicks on a blue circle on the map, which instantiates a prefab. This prefab has a script with OnGUI that loads a GUI.Window. The only thing I’m using to position the window on the screen is Input.mousePosition.

Using the screen captures below the first image shows when I click on the blue circle it shows the position is x:283, y:322. The x is always correct but the Y is always below for this circle. For other circles on the map it also may appear above where clicked.

In the second screen capture if I drag the window to where I think it should appear it shows the new position of the window is x:283, y:145. The prefab game object that is instantiated is set to a position of 0,0,0 (if that matters).


Code I’m using on the blue circles to instantiate the prefab (abWindow is a public variable assigned to the prefab)

function OnMouseUp () {	
        Instantiate(abWindow, transform.position, transform.rotation);
}

In the prefab I’m using this code:

function Awake () {
	var screenPos : Vector3 = Input.mousePosition;		
	print("Screen Position:" + screenPos);		
	objectWindow = Rect(screenPos.x,screenPos.y, winWidth,200);		
}

function OnGUI () {		
	objectWindow = GUILayout.Window (windowid, objectWindow, handleWindow, "Selected Object");			
}

function handleWindow (windowID : int) {	
	GUILayout.Label("Window is at: " + objectWindow.x + " : " + objectWindow.y);
	
	if (GUI.Button(Rect(100, 150, 100, 25), "Close") ) {		
		Destroy(this.gameObject);
	}
        GUI.DragWindow();
}

Thanks for any help,
Brent

MousePosition.y is zero at the bottom, while GUI is zero at the top.

raaaaaage

To fix it, you just

raaaaaage
*ahem

To fix it, you just need to subtract the mouse position from Screen.Height when creating the window.

Thanks so much… that did it. I’m new to the forums so what is raaaage ?

That’s me being angry that mouseposition and gui are built upside-down of each other. I’ve dealt with the same problem in a few other languages, and while there’s usually a reason for it, it still seems like nonsense. And it usually takes me a half hour or so to remember that oh, right, I have to invert one of the vertical components. And it’s annoying. :slight_smile:

Ah, understood… thanks again

You have to excuse Vicenti. He’s a very passionate person. But he’s also an excellent programmer, and if he answers one of your posts, chances are that he solved your problem.

* cheers! *

hiccup
burp

:slight_smile:

You can simply use Event.current.mousePosition, then it isn’t upside-down. You should always use Event functions with OnGUI, never Input.

–Eric

Thanks for the Tip Eric I’ll keep that in mind. I’m only using mousePosition during Awake to get the initial click location not within OnGUI.

What’s wrong with this poor Input.mousePosition ?

As complained about above, the Y coordinates are inverted because they are in screen space coordinates. Event.current.mousePosition uses GUI space coordinates.

–Eric