I'm trying to drag and drop items on screen space canvas. This is closest I've got it working.
It offsets more and more when you get further away from starting point of drag. I think I'm missing some scaling in positioning the UI-element?
I want it to stay right on mouse position on screen when I drag the UI-item. I'm using screen space canvas, because I want to have 3D-elements in the UI too.
This is from UI-element I'm trying to drag:
public void OnDrag(PointerEventData eventData)
Vector3 pos = new Vector3(Input.mousePosition.x - Screen.width/2,
Input.mousePosition.y - Screen.height/2, 0);
transform.localPosition = pos;
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
133 People are following this question.
Problem moving a GameObject with an OnDrag event when the canvas is in world space
C# - Creating Canvas UI button on click in code
Help Regarding Making A Server List by Instantiating prefabs
Unity UI Events not working for Canvas objects
Scale a canvas based on reference camera viewport