Quick answer: Use get_viewport().get_mouse_position() to get coordinates in the correct viewport. For world-space coordinates, use get_global_mouse_position() on a Node2D. Inside a SubViewport, you must account for the SubViewportContainer offset and scale.
Here is how to fix Godot viewport mouse position offset. You draw a cursor at the mouse location and it appears shifted by a fixed amount. You click an Area2D at the pointer and the click lands elsewhere. You set up a minimap in a SubViewport and its mouse handling is off by the container position. Godot has four separate mouse-position getters, each measuring in a different space, and picking the wrong one is a common source of subtle offset bugs.
The Symptom
A cursor follows the mouse but appears shifted by the title bar height, or by some fixed constant. A click registers at a position different from where you see the pointer. A SubViewport-based minimap reports mouse positions relative to the main window instead of the minimap contents.
Variant: positions are correct in windowed mode but offset in fullscreen or after resizing. Or positions scale correctly but are translated by a fixed margin.
What Causes This
Wrong getter for the space. Godot has:
Input.get_mouse_position()— raw screen coordinates in the windowget_viewport().get_mouse_position()— coordinates in the viewport reference size (after stretch)Node2D.get_global_mouse_position()— world-space coordinates accounting for camerasCanvasItem.get_local_mouse_position()— coordinates relative to this node’s transform
Using the wrong one for your task produces a consistent offset.
Stretch mode. Project Settings > Display > Window > Stretch > Mode can be disabled, canvas_items, or viewport. viewport renders at a fixed internal size and stretches. Mouse coordinates returned by viewport methods are in this logical size, not pixels.
SubViewport without a SubViewportContainer transform. Placing a SubViewport in a scene and displaying it somewhere creates a mismatch: the mouse events you receive are in the display space, but drawing inside the SubViewport uses SubViewport-space. Without explicit transformation, cursor sprites appear offset.
Camera2D zoom. A Camera2D with zoom != 1.0 changes the mapping between screen and world. get_global_mouse_position accounts for the active camera; get_viewport().get_mouse_position() does not.
Window mode vs exclusive fullscreen. Exclusive fullscreen may report coordinates without the title-bar offset, while windowed mode includes it from some APIs.
The Fix
Step 1: Use the right getter.
extends Node2D
func _process(_delta):
# World-space mouse (respects camera zoom and position)
var world_pos = get_global_mouse_position()
# Viewport-space (ignores camera)
var view_pos = get_viewport().get_mouse_position()
# Local to this node's transform
var local_pos = get_local_mouse_position()
print("world ", world_pos, " view ", view_pos, " local ", local_pos)
For positioning a cursor sprite in the world, use get_global_mouse_position. For Control-node UI positioning, use get_viewport().get_mouse_position(). They are not interchangeable.
Step 2: Set the stretch mode you actually want. In Project Settings:
- disabled — raw pixels. Mouse = pixel coords.
- canvas_items — CanvasItems scaled to fit. Mouse = reference-size coords.
- viewport — whole viewport scaled. Mouse = reference-size coords, pixels may be blurry.
For pixel-art games, use canvas_items with integer stretch mode. The viewport reports logical coordinates, which is what you want for gameplay.
Step 3: Map mouse into a SubViewport. Wrap the SubViewport in a SubViewportContainer and use its transform to convert the outer mouse position:
extends SubViewportContainer
func _gui_input(event: InputEvent):
if event is InputEventMouseMotion:
var sub = $SubViewport
# event.position is already relative to this container
var in_sub = event.position * sub.size / size
sub.push_input(event.duplicate())
Setting SubViewportContainer.stretch = true auto-forwards mouse events with correct scaling. Prefer this over manual math when you can.
Step 4: Account for Camera2D zoom when reading input. get_global_mouse_position already handles zoom. If you are reading Input.get_mouse_position() manually, multiply by the inverse of the canvas transform:
var screen = Input.get_mouse_position()
var world = get_canvas_transform().affine_inverse() * screen
Debugging the Offset
Draw a debug label at the mouse position and compare to what your game thinks is the mouse position:
func _draw():
var p = get_global_mouse_position() - global_position
draw_circle(p, 6, Color.RED)
If the red circle follows the cursor exactly, your math is right. If it sits at a fixed offset, you are using the wrong coordinate space.
“Four mouse-position getters. Four different spaces. Pick the one that matches where you want to draw.”
Related Issues
For UI input handling, see Area2D Not Detecting StaticBody2D. For stretch and scaling concerns across displays, Label3D Render Depth Wrong covers related 3D display topics.
get_global_mouse_position for world, get_viewport for UI, SubViewport needs a container. Coordinates line up.