Godot Version
Godot 4.3
Question
I implemented the drag function with the following code (this is the code provided in the official documentation). However, I found that when dragging quickly, the position of the dragged object will lag behind the position of the mouse. Are there any optimization methods to make the dragged object closely follow the mouse?
extends Node
var dragging = false
var click_radius = 32 # Size of the sprite.
func _input(event):
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
if (event.position - $Sprite2D.position).length() < click_radius:
# Start dragging if the click is on the sprite.
if not dragging and event.pressed:
dragging = true
# Stop dragging if the button is released.
if dragging and not event.pressed:
dragging = false
if event is InputEventMouseMotion and dragging:
# While dragging, move the sprite with the mouse.
$Sprite2D.position = event.position