Is it possible to lock the location of the mouse pointer on the game screen?

Godot Version

4.2.2 mono


Is it possible to lock the location of the mouse pointer on the game screen?
That is, if the mouse stops for example on the point (5,6). When the camera is moving to this point, the mouse pointer will remain fixed on this point (not moving with the camera - which is normally the case).
In 2D scene

You can use Input.wrap_mouse if supported, you’d need to compute the movement and then reset it

For the center of the screen you can use Input.mouse_mode

I used the following code, but it moves the mouse pointer out of the game and to the top and left of the monitor!

	Input.MouseMode = Input.MouseModeEnum.Visible;

According to my research, I came to the conclusion that this code does not work in FullScreen mode, but the settings are set to Windowed mode.

Last my 2D project my move looks glitchy in normal full screen. WindowMode.Fullscreen woks better.

to remove gray border

I think good solution to your problem is make virtual cursor.

I am new to game development and godot. Can you explain more? Should I apply these in the project settings or in the game logic?
In general, the camera follows the mouse pointer. Normally, the mouse stays on the screen, so the camera is always moving, to avoid that, I want the mouse to be locked in the viewport to be in the middle of the camera. I wrote the above two lines of code to lock the mouse at the mouse coordinates which is stationary. But instead of the mouse being locked in the desired coordinates, it is locked outside the game window and on the upper left side of the monitor.


using Godot;

using System;

public partial class MouseCamera : Node2D
	private Vector2 _mouseDeadzone = new Vector2(150, 150); // اندازه deadzone
	private Vector2 _lastMousePosition;
	private Vector2 _screenSize;
	private Vector2 _screenCenter;
	private Marker2D _lastMousePositionMarker;
	private Camera2D _camera2D;
	public override void _Ready()
		_camera2D = GetNode<Camera2D>(nameof(Camera2D));
		_camera2D.PositionSmoothingEnabled = true;
		_camera2D.PositionSmoothingSpeed = 1f;
		_screenSize = GetViewportRect().Size;
		_screenCenter = _screenSize / 2;
		_lastMousePositionMarker = GetNode<Marker2D>("LastMousePosition");
	public override void _Process(double delta)
		Vector2 currentMousePosition = GetGlobalMousePosition();

		if (currentMousePosition == _lastMousePosition) // در اینجا، ماوس حرکت نمی‌کند
			if (!IsMouseInDeadzone(GetViewport().GetMousePosition()))
				// تنظیم موقعیت نشانگر موس
				Input.MouseMode = Input.MouseModeEnum.Visible;
			// محاسبه جهت و حرکت دوربین
			_camera2D.Position.Lerp(currentMousePosition, 5f);
		else// در اینجا، ماوس در حال حرکت است
			_lastMousePositionMarker.Position = currentMousePosition;


		_lastMousePosition = currentMousePosition;
	private bool IsMouseInDeadzone(Vector2 mousePos)
		return Math.Abs(mousePos.X - _screenCenter.X) < _mouseDeadzone.X && Math.Abs(mousePos.Y - _screenCenter.Y) < _mouseDeadzone.Y;

In the scene, there is a Nod2D as the root, whose subset is a Marker2D and a Camera2,D to which I have given the above script.

it was C# code, if you set Clear Color to black in setting, working in editor can be difficult. somewhere in your game there you want support change screenmode?

this mouse mode is not what you looking?

Yes, I put an image for the background to make it clear. Yes, I want it to support changing the screenmode (but I don’t know yet). from InputEventMouseMotion.Relative; I used it but I still have a problem