Drag item from inventory and follow mouse position
Hello everyone,
I have been looking online for a solution for the last two days but I can't figure out what is going wrong. I am following along with a tutorial in order set up a inventory where you can drag items from one slot to another. However for some reason whenever I try to drag the item it just spawns in the bottom left of the UI Canvas.
I am using the new input system and my UI is a canvas with Screen space - Overlay. The code to move the inventory item looks like this:
private void OnDrag(GameObject obj)
{
if (_mouseItem.obj != null)
{
_mouseItem.obj.GetComponent<RectTransform>().localScale = new Vector3(0.1f, 0.1f, 1);
RectTransformUtility.ScreenPointToLocalPointInRectangle(
ui.transform as RectTransform,
new Vector2(MousePosition.x, MousePosition.y), ui.worldCamera,
out var move
);
_mouseItem.obj.GetComponent<RectTransform>().position = ui.transform.TransformPoint(move);
}
}
So far things I tried:
Switching from Screen space - Overlay to Screen space - Camera
Tried out different functions in the RectTransformUtility library
For some reason it feels like i am using the wrong setting at some location but I have no idea where to look further.
Any ideas?
Screenshot: https://ibb.co/wN7GTNj
Mentioned tutorial: https://www.youtube.com/watch?v=ZSdzzNiDvZk&t=1994s
Hi. Try this link, for me it works: https://youtu.be/h-2HwlGHfig
The IPointer interfaces fixed the issue for me. Thanks!