+1 vote

In process(delta) I get the cursor position the make it the sprites position but with a little offset. When I move the cursor really fast the sprite lags behind. I want it to stay perfectly next to the cursor no matter how quick the cursor is moving. How can I accomplish that?

asked Aug 1 in Projects by jujumumu (123 points)

2 Answers

0 votes
Best answer

I has the same trouble.
At last I find a trick.
1)Hide the system cursor
Input.setmousemode(Input.MOUSEMODEHIDDEN)
2)create a cursor node, with two child node:
a) a cursor image like the system cursor
b) a sprite which is changeful

Without the reference substance(system cursor), the cursor and sprite moves at the same time. And players almost can not perceive the delay.

answered Aug 2 by looming (46 points)
selected Aug 2 by jujumumu
+1 vote

This is a surprisingly hard thing to do.
There are multiple possible reasons for this lag:

One is that your mouse might be updated by the OS before Godot even gets to process the event that it moved. You can even notice the problem on sliders or windows of the OS itself, they never really are perfectly lined up with the mouse when you drag.

Second, graphics card are client-server accelerated graphics, which makes them a tiiiny bit longer to return a result than a direct (but slower) update of the screen data. I suspect the mouse does not even use accelerated graphics.

Third, Godot uses OpenGL double-buffering (triple?) to avoid flickering, which inevitably introduces one frame of lag. Desktop compositing might also play a role in this.

Fourth, Godot runs at 60 frames per second, so the time at which the result happens on screen is further delayed. You can reduce that time a little by not capping framerate and disabling v-sync, but there will still be a small delay.
Of course if your game runs too slow, that gap will increase but should then be fixed by optimizing the game.

If you want this for a custom mouse cursor, I think there are limited ways to get this working, but otherwise eliminating lag on a graphic following the mouse can be really hard, to my knowledge.

answered Aug 2 by Zylann (18,389 points)
edited Aug 2 by Zylann

You can define a custom mouse cursor in the Project Settings ("hardware cursor") or set it using the Input.set_custom_mouse_cursor() method), which won't suffer from an one-frame lag compared to a "software" cursor. This is true even if V-Sync is disabled.

So is there no way to do this?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.