Drag and Dropping gameobjects in worldspace

I am having an issue with my drag and drop. I am making a mostly UI driven game, although there will be some 3d animations below the main UI and so I wanted to use Worldspace or Camera so that way I can show things outside of just my UI.

If I have my canvas in Screen Space Overlay things drag just fine. The moment I try to use WorldSpace or Screen Space Camera things start to get wacky. The coordinates just go all over the place. I have tried using Camera.main.ScreenToWorldPoint to transform the position but that just seems to give me bad data as well. Is there something I’m missing here?

    public void OnBeginDrag(PointerEventData eventData)
    {
        StartDragPosition = eventData.position;
        difference = StartDragPosition - this.transform.position;
    }

    public void OnDrag(PointerEventData eventData)
    {
        //This offset helps us keep our mouse position on the object itself
        transform.position =  eventData.position - new Vector2(difference.x, difference.y);
    }

not sure, maybe you should use RectTransformUtility to calculate ui-position

1 Like

This led me to the correct answer at
https://stackoverflow.com/questions/37244471/click-and-drag-a-gameobject-in-a-overlay-canvas-in-unity

Thank you!

1 Like