I'm working on a game in Godot and I'm trying to create a system where I can click and hold onto a sprite and drag to move that same sprite but I'm having a bit of trouble figuring it out.
I've tried solutions such as looking for a mouse node, looking for mouse signals etc but couldn't find anything for either of them. I've also tried checking for coordinates of the mouse and moving it there but couldn't find a way to do this either. I've been trying to figure this out for a while so I'd appreciate the help.
Godot built-in drag and drop support only exist for
Controls not forNode2D(such asSprite2D). So, first consider if you actually need aSprite2Dor wouldTextureRectbe better. TheSprite2Dis a graphic object in the 2D game world, and theTextureRectis a graphic object in the UI.Now, if you really want to drag an
Sprite2D... We can break the problem twofold:Sprite2D(for which I recommend using anArea2D). See also How can I detect clicks on an AnimatedSprite2D in Godot with C#?Sprite2Dfollow the pointer... The simplest implementation would be changing theglobal_positionofSprite2Dtoget_global_mouse_positionin_process.