Set mouse position

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By trafel
:warning: Old Version Published before Godot 3 was released.

Is it possible to set the mouse position in Godot ?

Just by curiosity, what kind of gameplay that uses such feature are you going to implement? This could annoy the user very quickly if it gets in his way.

Gokudomatic2 | 2016-07-05 14:00

There exist many legitimate uses - for example, in an FPS - to keep the mouse in the center of the screen :stuck_out_tongue:

keke | 2018-04-09 22:37

@keke I would use Input.set_mouse_mode for that.

Lightning_A | 2021-03-28 05:02

Personally I’m using it to reset the mouse position after the user clicks and drags to rotate the camera in a map editor. Otherwise every time they rotated the camera the mouse would reappear somewhere else and they’d have to spend a split second searching for it and repositioning it.

Aiet | 2021-12-08 01:28

:bust_in_silhouette: Reply From: Akien

I think (untested) that get_viewport().warp_mouse(Vector2 to_pos) is meant for this purpose. The docs about it need some more detail (and a typo fix), but looking at the source code it seems to “warp”/relocate the mouse pointer.

There’s also Input.warp_mouse_pos(Vector2 to), which uses global coordinates.

Hinsbart | 2016-07-05 12:28

Works great, thanks !

trafel | 2016-07-05 15:33

I don’t know if yours was a typo or if it was changed, but the actual method is Input.warwarp_mouse_position(Vector2 to)

gneurshk | 2021-10-08 07:21

I assume warwarp is a typo, it should be

Input.warp_mouse_position( Vector2 to )

Input — Godot Engine (4.x) dokumentaatio suomeksi

leopeltola | 2021-11-16 16:33

:bust_in_silhouette: Reply From: lukifah

use

Input.warp_mouse_position(get_viewport().get_mouse_position()+movement)

movement is the Vector2 after getting joystick input strength

on ready you can

func ready() → void:
Input.setmousemode(Input.MOUSEMODE_CONFINED)