I’m trying to implement simple dragging logic. Everything works fine, but when I start dragging after the initial mouse button press, I’ve noticed there a slight lag. I’ve tried to log the values, and get_global_mouse_position() simply doesn’t get updated much during the first ± sec, after that, the dragging is smooth.
Any ideas?
Here is my most simple code snippet.
using Godot;
using System;
public partial class Icon : Sprite2D
{
private bool _isHovered = false;
private bool _isDragging = false;
private Vector2 _dragOffset;
public override void _Ready()
{
var area = GetNode<Area2D>("%Area2D");
area.MouseEntered += OnMouseEntered;
area.MouseExited += OnMouseExited;
}
public override void _Process(double delta)
{
if (_isDragging)
{
GD.Print(GetGlobalMousePosition());
GlobalPosition = GetGlobalMousePosition();
}
}
public override void _Input(InputEvent @event)
{
if (@event is InputEventMouseButton mouseEvent)
{
if (mouseEvent.Pressed && mouseEvent.ButtonIndex == MouseButton.Left && _isHovered)
{
GD.Print("Pressed");
_isDragging = true;
}
else if (!mouseEvent.Pressed && mouseEvent.ButtonIndex == MouseButton.Left)
{
_isDragging = false;
GD.Print("Released");
}
}
}
private void OnMouseEntered()
{
GD.Print("Entered");
_isHovered = true;
}
private void OnMouseExited()
{
GD.Print("Exited");
_isHovered = false;
}
}
Hmm, I have an idea - it seems like the Shape2D in your CollisionShape2D node is really small, because you are exiting and entering the shape during the drag. Can you try to make it bigger and see if this helps in any way?
Gave it a try and that was not the reason. But if you check the logs, you can see that they show the same positions for GetGlobalMousePosition in the first few logs (thus the lag in my opinion).
My uneducated guess would be that the processing of mouse movement is somehow delayed after the first click for some reason, cuz I’m always getting the same value when Process functions runs, even though the mouse is clearly in different position.
Is your Mac above the minimum requirements?
I dint really see any other explanation for it, sorry. Maybe you want to report it on the Godot GitHub as a bug.
I had this same exact issue while trying to implement a basic drag action for a solitare game. I noticed that after clicking, the card’s position wouldn’t update for 200-300 ms but then would move to the mouse’s location and follow it around just fine.
I am also on a mac (m1 macbook pro) & Godot 4.3. The solution for me was to close BetterTouchTool. I also saw related threads saying that Magnet can also cause issues.