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
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.
code:
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;
Input.WarpMouse(_lastMousePositionMarker.Position);
}
// محاسبه جهت و حرکت دوربین
_camera2D.Position.Lerp(currentMousePosition, 5f);
}
else// در اینجا، ماوس در حال حرکت است
{
_lastMousePositionMarker.Position = currentMousePosition;
GD.Print(currentMousePosition);
GD.Print(IsMouseInDeadzone(GetViewport().GetMousePosition()));
}
_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?
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