- Edited
I'm new to GoDot, working on a simple game to get me familiar with it, but I need a script that will allow me to click the sprite, grab it and then release it when it's clicked.
I'm new to GoDot, working on a simple game to get me familiar with it, but I need a script that will allow me to click the sprite, grab it and then release it when it's clicked.
Try this code from this old topic.http://godotdevelopers.org/index.php?topic=14035.msg14035#msg14035
This link seems dead.
Is there a simple way to drag&drop Sprite nodes ?
There should be since it's such a basic functionality and Sprites already have the boolean "pickable", but I can't find one.
Here's an example of a script you'd attach to a node that's a child of your sprite which, as long as the node is clickable should move around your sprite.
extends Control # could be any node with the on_gui_input signal
var can_drag := true # set this yourself with other code if you like
var is_dragging := false
func _on_gui_input(event: InputEvent) -> void:
if event is InputEventMouseButton:
is_dragging = event.pressed
if event is InputEventMouseMotion and can_drag:
var length : float = event.relative.length()
if is_dragging and length < 180.0:
var n : Node2D = get_parent()
var movement : Vector2 = event.relative.rotated(n.rotation)
movement.x *= n.scale.x
movement.y *= n.scale.y
n.global_position += movement
Dragging and dropping is typically not so simple if you're expecting the objects to have some behavior such as snap to grid.
I built a system that may be similar to what you need. Basically, all items that can be "dragged and dropped" have an invisible Button node, and when that button is held down the item follows the mouse movement.