I wrote the following code for drag and drop:
func _input(event):
if event is InputEventMouseButton:
if event.is_pressed() && mouse_entered:
draggingDistance = position.distance_to(get_viewport().get_mouse_position())
dir = (get_viewport().get_mouse_position() - position).normalized()
dragging = true
else:
dragging = false
elif event is InputEventMouseMotion:
if dragging:
position = get_viewport().get_mouse_position() - draggingDistance * dir
pass
where mouseentered and mouseexisted are mouse events.
I am having trouble stopping the object from moving with the mouse if it collides
What I tried so far:
func _process(delta):
var collided = move_and_collide(Vector2(0, 0))
if collided != null:
dragging = false
pass
It doesn't work as I really want it to be. It's glitchy and if I moved the mouse fast it just gets to the other side of the object it collided with.
1: desired movement

2: when I move the mouse fast:

any idea how I can do this in a clean way?
Thank you,