Fix Mouse Position Lag

Godot Version

4.2.2

Question

One basic problem I’ve been running into a lot with Godot is properly using the mouse position. For some reason there seems to be a slight delay between the position being used and the actual mouse position.
Here’s a basic example:


The Godot logo is using this code to move to the mouse position:

func _process(delta):
	position = get_window().get_mouse_position()

The arrow is using this code to point towards the mouse:

func _process(delta):
	look_at(get_window().get_mouse_position())

I can’t figure out why both objects are lagging behind the mouse, or how to fix it.

2 Likes

can you try to use get_global_mouse_position() instead of get_window().get_mouse_position()?

I just tried that but it unfortunately doesn’t change anything.

can you put this code in _physics_process()?

That doesn’t seem to work either.

Thats weird. Are there any changes you made in this project or is this an empty project with just does two lines of code?

This is inherent to how the OS mouse cursor is drawn compared to any other window, and is not an issue exclusive to Godot. You can’t fix this entirely unless you replace the mouse cursor image with the sprite’s texture, which you can do using OS.set_custom_mouse_cursor(). The arrow will still appear to lag behind slightly though, which you could mitigate by making look_at() point towards the extrapolated (predicted) mouse cursor’s position instead of its current mouse position. Doing so will introduce some visible error when the mouse cursor speed changes quickly though, so the result may look shaky in motion.

If you can’t use hardware mouse cursors for some reason, see the suggestions to reduce Input lag in the documentation.

2 Likes