OnDrag problem

Hi

I’m trying to drag items inside UI panel (inventory system). Methods for drag event:

  public void OnBeginDrag(PointerEventData eventData)
  {
  itemBeingDragged = gameObject;
  startDraggingPosition = transform.position;
  }

  public void OnDrag(PointerEventData eventData)
  {
  transform.position = eventData.position;
  Debug.Log("Start position: " + startDraggingPosition + " currentPosition: " + transform.position + " mousePosition: " + Input.mousePosition);
  }

When I start to drag item, I get ‘normal’ debug log:
2617401--183683--unityProblem1.png

However item disappears, and transform shows why:
2617401--183684--unityProblem2.png

Conclusion: somehow item’s position is updated to very strange one
Questions:

  1. What did I miss? (tried even ScreenToWorldPoint but it didn’t work - here’s GUI element so I suppose it shouldn’t work)
  2. Is there a better/more practical way to do items dragging in UI inventory?

you appear to be trying to set the transform’s world position to one taken from screenspace…

have a look at the code and bored’s tutorial on this thread :slight_smile:

Thanks! Tutorial that I found was missing camera z position :slight_smile: