'UI in world space drag and drop does not align with mouse

Im doing a UI that is in World Space and i am getting strange behaviour when doing drag and drop.

https://i.imgur.com/MBZ8gNH.mp4

This is the code:

public void OnDrag(PointerEventData eventData)
{
    if (!image.enabled) return;

    var newPosition = rectTrans.anchoredPosition + eventData.delta * canvas.scaleFactor;
    var newX = Mathf.Clamp(newPosition.x, leftSibling.x, rightSibling.x);
    var newY = Mathf.Clamp(newPosition.y, 0, height);

    rectTrans.anchoredPosition = new Vector2(newX, newY) / canvas.scaleFactor;
}


Solution 1:[1]

This one solved it for me:

reactTranform.anchoredPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 manish ma