Godot handles mouse movement input using input events and the Input class—no special setup needed beyond writing code in a script attached to a node. This allows you to track cursor position, implement camera control, UI interaction, or pointer-based gameplay mechanics. To get started, you'll need a Godot project open in the editor, a node (like Node2D or Control) with a script, and basic familiarity with GDScript.

What You'll Need

  • Godot Engine (version 4.x recommended; steps verified on 4.2+)
  • A new or existing project with at least one scene containing a visible node
  • A GDScript file attached to a node in the scene
  • Basic understanding of Godot's node system and signal connections (optional but helpful)

Step-by-Step

  1. Create or open your project in Godot. In the FileSystem dock, right-click the root folder of your project (or a subfolder) and select New Scene. Add a Node2D (for 2D) or Node3D (for 3D) as the root. Save the scene (e.g., as main.tscn).
  2. Attach a script to the root node. Click the node in the Scene dock, then click the Attach Script icon (the sheet with a + sign) in the Inspector. Choose GDScript, set the path (e.g., res://scripts/main.gd), and click Create.
  3. Use _input(event) to capture mouse movement. In your script, define the _input(event) function. This method receives all input events, including mouse motion. Inside it, check if the event is of type InputEventMouseMotion. Here's a minimal working example:
    func _input(event):
        if event is InputEventMouseMotion:
            print("Mouse moved to: ", event.position)
    This will print the cursor's position (relative to the viewport) every time the mouse moves.
  4. Track relative movement instead of absolute position if you're building camera controls or want delta motion. The InputEventMouseMotion object has both position (absolute coordinates) and relative (change since last frame). For smoother camera panning, use event.relative:
    var camera_offset := Vector2.ZERO
    
    func _input(event):
        if event is InputEventMouseMotion:
            camera_offset += event.relative * 0.5  # Adjust sensitivity
    Apply this offset to your camera node in _process() or via a Camera2D node's offset property.
  5. Use Input.get_mouse_position() for real-time queries outside of _input. If you need the current mouse position during logic updates (e.g., in _process()), call Input.get_mouse_position():
    func _process(delta):
        var mouse_pos := Input.get_mouse_position()
        $Sprite2D.position = mouse_pos
    This directly sets a Sprite's position to follow the cursor every frame.
  6. Enable relative mouse mode for FPS-style control (e.g., to lock the cursor and hide it). Call Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) to capture the cursor and make it invisible. This is essential for 3D first-person cameras:
    func _ready():
        Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
    To release the cursor (e.g., on ESC press), use Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) inside a _input() handler for InputEventKey with KEY_ESCAPE.
  7. Connect UI interactions with mouse events for buttons or controls. For UI elements, use signals like mouse_entered and mouse_exited on Control nodes:
    func _on_Button_mouse_entered():
        $Button.add_theme_color_override("font_color", Color(1, 0, 0))
    
    func _on_Button_mouse_exited():
        $Button.add_theme_color_override("font_color", Color(0, 0, 0))
    Connect these signals in the Inspector (select the button > Node tab > signals > double-click the signal to create a handler).

Common Mistakes

  • Forgetting to check the event type—if you only use Input.get_mouse_position() without filtering in _input(), you'll process non-motion events (like key presses) and waste performance or cause errors.
  • Using _process instead of _input for motion events—mouse motion isn't polled in _process; it's event-driven. Relying solely on _process with Input.is_action_just_pressed() for mouse movement won't work; you need _input or Input.get_mouse_position().
  • Ignoring coordinate systems—in 2D, Input.get_mouse_position() returns viewport-relative coordinates (0,0 at top-left of the window). If you use Camera2D, your world coordinates differ; convert with get_viewport().get_mouse_position() (for world space) or camera.unproject_position() (for 3D).
  • Not handling captured mode properly—in MOUSE_MODE_CAPTURED, the mouse disappears and movement is only reported as relative delta. If you try to read event.position in this mode, it returns the last known absolute position before capture, which may be stale. Use event.relative instead.
  • Overlooking platform differences—on mobile, mouse input doesn't exist (touch is used instead). If testing on Android/iOS, mouse events won't fire. Always test on your target platform.

Tips

  • Use InputMap for mouse buttons, not motion—define actions like ui_click in Project > Settings > Input Map for left/right clicks, but mouse movement is best handled directly via events.
  • Smooth camera movement—add a lerp or damp function to camera_offset to avoid jitter:
    camera_offset = camera_offset.lerp(target_offset, 0.2)
  • Prevent cursor drift in captured mode—reset the cursor to the center of the screen each frame using Input.warp_mouse_position() (Godot 4.2+). Example:
    func _input(event):
        if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
            # Accumulate delta for camera
            look_y -= event.relative.y * sensitivity
            look_x += event.relative.x * sensitivity
            Input.warp_mouse_position(get_viewport().size / 2)
  • Debug with print() and the Output panel—Godot's Output dock shows all print statements. Watch for coordinate jumps when switching mouse modes.
  • For 3D, use PhysicsServer for raycasting—combine mouse position with Camera3D.project_ray_normal() and PhysicsDirectSpaceState3D.intersect_ray() to pick objects under the cursor.

Troubleshooting

  • No output when moving the mouse—First, verify your script is attached to a node that's in the active scene tree. Second, ensure the window has focus (click inside the Godot editor window or the running game window). Third, check if Input.is_action_just_pressed("ui_accept") works—if not, your InputMap may be misconfigured, but mouse motion doesn't use InputMap, so focus on event handling.
  • Coordinates seem wrong or inverted—if your camera moves opposite to mouse direction, flip the sign of event.relative.x or event.relative.y. Also confirm whether your coordinate system expects world or screen space (e.g., get_viewport().get_mouse_position() gives screen space; camera.unproject_position() gives world).
  • Mouse locks up or cursor is stuck—if you accidentally set MOUSE_MODE_CAPTURED and can't get it back, add a key handler (e.g., ESC) to reset to MOUSE_MODE_VISIBLE. In development, restart the scene or Godot to clear the state.
  • Touch devices show no motion—on mobile, use InputEventScreenTouch and InputEventScreenDrag instead. Replace InputEventMouseMotion checks with event is InputEventScreenDrag and use event.position.
  • Performance lag with frequent prints—remove or comment print() statements in release builds. Instead, use OS.is_debug_build() to guard logs or use push_warning() for non-intrusive messages.

Frequently Asked Questions

Does Godot support relative mouse movement out of the box?

Yes. The InputEventMouseMotion class includes a relative property that gives the movement delta since the last frame, regardless of screen resolution or window size.

Can I use mouse input in 3D scenes?

Yes, but you'll typically combine InputEventMouseMotion for camera control with raycasting (using Camera3D.project_ray_normal()) for object interaction. In 3D, mouse movement often controls camera rotation, while clicks handle selection.

How do I make the mouse cursor visible again after capturing it?

Call Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) when needed (e.g., when opening a pause menu). This restores the cursor and releases the window capture.

Why does Input.get_mouse_position() return (0,0) on startup?

Because the mouse hasn't moved yet. Godot initializes the position to (0,0) until the first motion event. Call Input.get_mouse_position() only after user interaction or use a fallback like the center of the viewport.

Is mouse movement affected by DPI scaling?

No—Godot reports mouse coordinates in pixels relative to the viewport, not scaled units. If your UI appears misaligned, check your project's Display > Window > Size > Mode setting (e.g., Viewport vs 2D) and ensure your Control nodes use proper anchors and margins.

Do I need to enable mouse input in project settings?

No. Godot enables mouse input by default. However, in Project > Settings > Input Devices > Pointer, ensure Mouse Cursor is not disabled (it's on by default). This setting only affects whether the OS cursor is shown, not event handling.