Simple 'Drag and Drop' along x-axis

Hi,

I need help with a small problem. I’d like to have a GUI element (in this case a window) that is draggable along the x-axis. I did it like this:

using UnityEngine;
using System.Collections;

public class DragTest : MonoBehaviour {

	public float yPosition = 0.0f;
	private Rect windowRect = new Rect (0, Screen.height/2 - 50, 100, 100);
	
	void OnGUI () {
		
		Event e = Event.current;
		
		Rect windowRect = new Rect (yPosition, Screen.height/2 - 50, 100, 100);
		windowRect = GUI.Window (0, windowRect, WindowFunction, "My Window");
		
		if (windowRect.Contains(Input.mousePosition)  e.type == EventType.MouseDrag) {
			yPosition = e.mousePosition.x;
        }
	}
	
	void WindowFunction (int windowID) {
		// Draw any Controls inside the window here
	}
}

I can actually drag it from the left to the right, but not into the other direction. Why is that and how can I solve the problem?

Thx in advance!

I just found a solution to my problem in this thread

But I am still not sure, why my approach caused a problem :frowning:

I’ve not played with the gui… but in line 13 you use Input.mousePosition and in 14 you use e.mousePosition … is that right?

yes, that’s right. But does it make a difference?

Input.mousePosition uses screen coordinates, and Event.current.mousePosition uses GUI coords, so that should always be used in OnGUI code.

–Eric