Movement of an object behind the mouse on a grid

Godot Version

Godot 3.5.3


I want my sprite to move with the mouse, but still respect a 32x32 grid. How do I do this?

Something tells me to use Vector2.snapped, but my math knowledge doesn’t allow me to figure it out.

could you write a small script with this action

The mouse movement script is like this:
global_position += event.relative * Camera2D.zoom

This should work regardless of the camera and zoom:

extends Sprite

func _unhandled_input(event: InputEvent) -> void:
  if event is InputEventMouseMotion:
    global_position = get_global_mouse_position().snapped(Vector2(32, 32))
1 Like