• 2D
  • There is a lag when make a item follow the mouse always how to eliminate the lag?

https://github.com/godotengine/godot-demo-projects/tree/master/2d/bullet_shower

I follow the project in this site.

The player always moves with the mouse. But I can feel a small lag when moving the mouse. About 0.1 seconds I think. Anyway to eliminate this?

The code in this project about follow the mouse is:

func _input(event):
	if event is InputEventMouseMotion:
 		position = event.position - Vector2(0, 16)

https://github.com/godotengine/godot-demo-projects/tree/master/2d/bullet_shower

I follow the project in this site.

The player always moves with the mouse. But I can feel a small lag when moving the mouse. About 0.1 seconds I think. Anyway to eliminate this?

The code in this project about follow the mouse is:

func _input(event):
	if event is InputEventMouseMotion:
 		position = event.position - Vector2(0, 16)

There is no way to avoid this lag when using a "software" mouse cursor (not without error-prone extrapolation at least).

To avoid lag due to the game updating with a 1-frame delay (compared to the operating system), you must use a "hardware" mouse cursor using Input.set_custom_mouse_cursor().

@Calinou said: There is no way to avoid this lag when using a "software" mouse cursor (not without error-prone extrapolation at least).

To avoid lag due to the game updating with a 1-frame delay (compared to the operating system), you must use a "hardware" mouse cursor using Input.set_custom_mouse_cursor().

Thank you I make it. Follow your advice about the mouse cursor. I do some google work, and get these things.

https://docs.godotengine.org/en/stable/tutorials/inputs/custom_mouse_cursor.html

https://github.com/guilhermefelipecgs/custom_hardware_cursor

The github link make a non-latency mouse move.

Oh, it still not solved yet. It seems got a latency in Input function,

I try to solve it by remove the Input function and make it into the _process function, but still not work. Why this happened?


extends Node2D
# This demo is an example of controling a high number of 2D objects with logic
# and collision without using nodes in the scene. This technique is a lot more
# efficient than using instancing and nodes, but requires more programming and
# is less visual. Bullets are managed together in the `bullets.gd` script.

# The number of bullets currently touched by the player.
var touching = 0
onready var sprite = $AnimatedSprite
var arrow = load("res://arrow.png")
var beam = load("res://beam.png")


func _ready():
	# The player follows the mouse cursor automatically, so there's no point
	# in displaying the mouse cursor.
#	Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
	Input.set_custom_mouse_cursor(arrow)

# remove this 
#func _input(event):
#	if event is InputEventMouseMotion:
#		position = event.position

# add this
func _process(delta):
	position = get_viewport().get_mouse_position()

func _on_body_shape_entered(_body_id, _body, _body_shape, _local_shape):
	touching += 1
	if touching >= 1:
		$AnimatedSprite.frame = 1


func _on_body_shape_exited(_body_id, _body, _body_shape, _local_shape):
	touching -= 1
	if touching == 0:
		$AnimatedSprite.frame = 0

@Calinou do you mean change the cursor like this? Seems not work.

Using _input works fine. But you want to use event.relative for movement code. There may be a 1 frame delay, but honestly it is unnoticeable. At 60Hz, this would be 17ms, which is honestly nothing.

@cybereality said: Using _input works fine. But you want to use event.relative for movement code. There may be a 1 frame delay, but honestly it is unnoticeable. At 60Hz, this would be 17ms, which is honestly nothing.

It is not unnoticeable for me.

I make some screenshot when moving the mouse, you can see the player center has a distance to the mouse

Well there must be a bug in your code then. That's not normal.

Here is an example project for how you do it.

If you see both the sprite and the hardware mouse cursor, you are not using the hardware mouse cursor as designed. When you want the sprite to follow the mouse cursor instantly, the sprite must be set as the hardware mouse cursor (and the original sprite must be hidden).