- Home /
Dragging GUI Item, get mouse to stay at part of box where drag began
So I'm trying to make a custom timeline and I have boxes that the user can place on the timeline and drag around. They can only be dragged from left to right. Now the problem is that when I drag them, instead of my mouse staying over the part where I started dragging, the beginning x of the rect jumps to my mouse. I would like the x of my rect to move the same amount of pixels as the mouse with my mouse staying in the part of the box that it originally starting dragging from. The boxes can be dragged from left to right. Now I have tried using
if(Event.currrent.type==EventType.MouseDrag&&MyBoxRect.Contains(Event.current.mousePosition){
MyBoxRect.x+=Event.current.delta.x;
}
and
MyBoxRect.x+=(Event.current.mousePosition.x-MyBoxRect.x);
and
MyBoxRect.x+=(Event.current.delta.x * 40);
None of these give the result I want. Delta.x results in my mouse moving pretty far from the box. Event.current.mousePosition.x-MyBoxRect.x results in the mouse always being at the start x of MyBoxRect. Now what would be the proper way to get my mouse to stay at the part of the box it started dragging at and to get the x to move only the amount of pixels that my mouse moved?
Answer by CoryButler · Jul 09, 2017 at 07:26 PM
You need to get the distance between the mouse and box X. Then subtract the distance from the mouse X.
var distance = Event.current.mousePosition.x - MyBoxRect.x;
MyBoxRect.x = Event.current.mousePosition.x - distance;
Yes this will be better. deltaX is a filtered value which can be adjusted in the preferences.
Unity doesn't support a lot of $$anonymous$$ouse stuff it seems to me. This is the reason a lot of games hide the real mouse and implement a virtual mouse ins$$anonymous$$d.
Hi Cory. I tried this but my box's do not move at all.
Answer by Cornelis-de-Jager · Jul 10, 2017 at 02:25 AM
First Ensure that the Anchor of the Rect Transform is in the bottom left corner (or Top Left)
/* Variables */
Vector2 offset;
bool moving;
/* Unity Scripting API */
void OnMouseDown () {
Vector3 tran = GetComponent<RectTransform> ().position;
offset = new Vector2 (Input.mousePosition.x - tran.x, Input.mousePosition.y - tran.y);
moving = true;
}
Update () {
// Button Released
if (Input.GetKeyUp(KeyCode.Mouse0))
moving = false;
// Button Down:
GetComponent<RectTransform> ().position = new Vector3 (Input.mousePosition.x + offset.x, Input.mousePosition.y + offset.y , 0);
}
Hi! This is the GUI system and not the UI system. I am making a editor tool so I am using OnGUI and GUI functions to draw everything. Now I adapted this code to work in OnGUI but it still doesn't work properly. The boxes are moving way ahead of my mouse and will only go to the right , they don't move to the left at all.
Have you tried subtracting offset.x ins$$anonymous$$d of adding it?
Your answer
![](https://koobas.hobune.stream/wayback/20220612125813im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using EditorSceneManager to make custom level skip editor GUI doesn't work 0 Answers
Is it possible to store and display EditorGUILayout.Toggles? 0 Answers
Custom Editor GUI Elements 1 Answer
Default toggle style 1 Answer
Lining up GUI 1 Answer