From c475ef95f0ed8b5c844242980ca3d5b76f8ed1f3 Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Sun, 3 Nov 2024 06:30:54 -0500 Subject: [PATCH] fix(Controllers): Use Input.mousePosition instead of GetAxisRaw --- .../PlayerTest/PlayerRBReliable.prefab | 1 + .../PlayerTest/PlayerRBUnreliable.prefab | 1 + .../Examples/PlayerTest/PlayerReliable.prefab | 1 + .../PlayerTest/PlayerUnreliable.prefab | 1 + .../Examples/PlayerTest/TankReliable.prefab | 1 + .../Examples/PlayerTest/TankUnreliable.prefab | 1 + .../FlyerController/FlyerControllerBase.cs | 17 +++++++++++++++-- .../PlayerController/PlayerControllerBase.cs | 18 ++++++++++++++++-- .../PlayerControllerRBBase.cs | 17 +++++++++++++++-- .../TankController/TankTurretBase.cs | 18 ++++++++++++++++-- 10 files changed, 68 insertions(+), 8 deletions(-) diff --git a/Assets/Mirror/Examples/PlayerTest/PlayerRBReliable.prefab b/Assets/Mirror/Examples/PlayerTest/PlayerRBReliable.prefab index a4c8471f6..dc9dc505b 100644 --- a/Assets/Mirror/Examples/PlayerTest/PlayerRBReliable.prefab +++ b/Assets/Mirror/Examples/PlayerTest/PlayerRBReliable.prefab @@ -469,6 +469,7 @@ MonoBehaviour: _mouseInputX: 0 _mouseSensitivity: 0 _groundState: 0 + _mousePosition: {x: 0, y: 0} _direction: {x: 0, y: 0, z: 0} _velocity: {x: 0, y: 0, z: 0} _controllerUI: {fileID: 0} diff --git a/Assets/Mirror/Examples/PlayerTest/PlayerRBUnreliable.prefab b/Assets/Mirror/Examples/PlayerTest/PlayerRBUnreliable.prefab index 000c94d3b..c3c48c01e 100644 --- a/Assets/Mirror/Examples/PlayerTest/PlayerRBUnreliable.prefab +++ b/Assets/Mirror/Examples/PlayerTest/PlayerRBUnreliable.prefab @@ -469,6 +469,7 @@ MonoBehaviour: _mouseInputX: 0 _mouseSensitivity: 0 _groundState: 0 + _mousePosition: {x: 0, y: 0} _direction: {x: 0, y: 0, z: 0} _velocity: {x: 0, y: 0, z: 0} _controllerUI: {fileID: 0} diff --git a/Assets/Mirror/Examples/PlayerTest/PlayerReliable.prefab b/Assets/Mirror/Examples/PlayerTest/PlayerReliable.prefab index 4a78c8ea4..a89d40a02 100644 --- a/Assets/Mirror/Examples/PlayerTest/PlayerReliable.prefab +++ b/Assets/Mirror/Examples/PlayerTest/PlayerReliable.prefab @@ -487,6 +487,7 @@ MonoBehaviour: _mouseInputX: 0 _mouseSensitivity: 0 _groundState: 0 + _mousePosition: {x: 0, y: 0} _direction: {x: 0, y: 0, z: 0} _velocity: {x: 0, y: 0, z: 0} _controllerUI: {fileID: 0} diff --git a/Assets/Mirror/Examples/PlayerTest/PlayerUnreliable.prefab b/Assets/Mirror/Examples/PlayerTest/PlayerUnreliable.prefab index d1c2b7d5b..ba31cc5e4 100644 --- a/Assets/Mirror/Examples/PlayerTest/PlayerUnreliable.prefab +++ b/Assets/Mirror/Examples/PlayerTest/PlayerUnreliable.prefab @@ -487,6 +487,7 @@ MonoBehaviour: _mouseInputX: 0 _mouseSensitivity: 0 _groundState: 0 + _mousePosition: {x: 0, y: 0} _direction: {x: 0, y: 0, z: 0} _velocity: {x: 0, y: 0, z: 0} _controllerUI: {fileID: 0} diff --git a/Assets/Mirror/Examples/PlayerTest/TankReliable.prefab b/Assets/Mirror/Examples/PlayerTest/TankReliable.prefab index d9d5c1360..d8303df1a 100644 --- a/Assets/Mirror/Examples/PlayerTest/TankReliable.prefab +++ b/Assets/Mirror/Examples/PlayerTest/TankReliable.prefab @@ -260,6 +260,7 @@ MonoBehaviour: _mouseSensitivity: 10 _lastShotTime: 0 _turretUI: {fileID: 0} + _mousePosition: {x: 0, y: 0} turretNetworkTransform: {fileID: 8662002890669114756} barrelNetworkTransform: {fileID: 4362334322449175123} --- !u!114 &8662002890669114756 diff --git a/Assets/Mirror/Examples/PlayerTest/TankUnreliable.prefab b/Assets/Mirror/Examples/PlayerTest/TankUnreliable.prefab index eacab4a56..bfeef9389 100644 --- a/Assets/Mirror/Examples/PlayerTest/TankUnreliable.prefab +++ b/Assets/Mirror/Examples/PlayerTest/TankUnreliable.prefab @@ -260,6 +260,7 @@ MonoBehaviour: _mouseSensitivity: 10 _lastShotTime: 0 _turretUI: {fileID: 0} + _mousePosition: {x: 0, y: 0} turretNetworkTransform: {fileID: 8518206564796711397} barrelNetworkTransform: {fileID: 470423508858832317} --- !u!114 &8518206564796711397 diff --git a/Assets/Mirror/Examples/_Common/Controllers/FlyerController/FlyerControllerBase.cs b/Assets/Mirror/Examples/_Common/Controllers/FlyerController/FlyerControllerBase.cs index 5e85aa229..1d8352f0b 100644 --- a/Assets/Mirror/Examples/_Common/Controllers/FlyerController/FlyerControllerBase.cs +++ b/Assets/Mirror/Examples/_Common/Controllers/FlyerController/FlyerControllerBase.cs @@ -161,6 +161,7 @@ public struct RuntimeData [ReadOnly, SerializeField, Range(-1f, 1f)] float _mouseInputX; [ReadOnly, SerializeField, Range(0, 30f)] float _mouseSensitivity; [ReadOnly, SerializeField] GroundState _groundState; + [ReadOnly, SerializeField] Vector2 _mousePosition; [ReadOnly, SerializeField] Vector3 _direction; [ReadOnly, SerializeField] Vector3Int _velocity; [ReadOnly, SerializeField] GameObject _controllerUI; @@ -239,6 +240,12 @@ public GroundState groundState internal set => _groundState = value; } + public Vector2 mousePosition + { + get => _mousePosition; + internal set => _mousePosition = value; + } + public Vector3 direction { get => _direction; @@ -313,6 +320,7 @@ public override void OnStartAuthority() // Calculate DPI-aware sensitivity float dpiScale = (Screen.dpi > 0) ? (Screen.dpi / BASE_DPI) : 1f; runtimeData.mouseSensitivity = turnAcceleration * dpiScale; + runtimeData.mousePosition = Input.mousePosition; SetCursor(controlOptions.HasFlag(ControlOptions.MouseSteer)); @@ -392,7 +400,7 @@ void Update() void SetCursor(bool locked) { - Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None; + //Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None; Cursor.visible = !locked; } @@ -436,8 +444,13 @@ void HandleTurning(float deltaTime) void HandleMouseSteer(float deltaTime) { + // Get mouse position, calculate delta + Vector2 currentMousePosition = Input.mousePosition; + Vector2 mouseDelta = currentMousePosition - runtimeData.mousePosition; + runtimeData.mousePosition = currentMousePosition; + // Accumulate mouse input over time - runtimeData.mouseInputX += Input.GetAxisRaw("Mouse X") * runtimeData.mouseSensitivity; + runtimeData.mouseInputX += mouseDelta.x * runtimeData.mouseSensitivity; // Clamp the accumulator to simulate key press behavior runtimeData.mouseInputX = Mathf.Clamp(runtimeData.mouseInputX, -1f, 1f); diff --git a/Assets/Mirror/Examples/_Common/Controllers/PlayerController/PlayerControllerBase.cs b/Assets/Mirror/Examples/_Common/Controllers/PlayerController/PlayerControllerBase.cs index 6d697bb34..3ffa7c8e6 100644 --- a/Assets/Mirror/Examples/_Common/Controllers/PlayerController/PlayerControllerBase.cs +++ b/Assets/Mirror/Examples/_Common/Controllers/PlayerController/PlayerControllerBase.cs @@ -125,6 +125,7 @@ public struct RuntimeData [ReadOnly, SerializeField, Range(-1f, 1f)] float _mouseInputX; [ReadOnly, SerializeField, Range(0, 30f)] float _mouseSensitivity; [ReadOnly, SerializeField] GroundState _groundState; + [ReadOnly, SerializeField] Vector2 _mousePosition; [ReadOnly, SerializeField] Vector3 _direction; [ReadOnly, SerializeField] Vector3Int _velocity; [ReadOnly, SerializeField] GameObject _controllerUI; @@ -185,6 +186,12 @@ public GroundState groundState internal set => _groundState = value; } + public Vector2 mousePosition + { + get => _mousePosition; + internal set => _mousePosition = value; + } + public Vector3 direction { get => _direction; @@ -260,6 +267,7 @@ public override void OnStartAuthority() // Calculate DPI-aware sensitivity float dpiScale = (Screen.dpi > 0) ? (Screen.dpi / BASE_DPI) : 1f; runtimeData.mouseSensitivity = turnAcceleration * dpiScale; + runtimeData.mousePosition = Input.mousePosition; SetCursor(controlOptions.HasFlag(ControlOptions.MouseSteer)); @@ -327,7 +335,7 @@ void Update() void SetCursor(bool locked) { - Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None; + //Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None; Cursor.visible = !locked; } @@ -372,8 +380,13 @@ void HandleTurning(float deltaTime) void HandleMouseSteer(float deltaTime) { + // Get mouse position, calculate delta + Vector2 currentMousePosition = Input.mousePosition; + Vector2 mouseDelta = currentMousePosition - runtimeData.mousePosition; + runtimeData.mousePosition = currentMousePosition; + // Accumulate mouse input over time - runtimeData.mouseInputX += Input.GetAxisRaw("Mouse X") * runtimeData.mouseSensitivity; + runtimeData.mouseInputX += mouseDelta.x * runtimeData.mouseSensitivity; // Clamp the accumulator to simulate key press behavior runtimeData.mouseInputX = Mathf.Clamp(runtimeData.mouseInputX, -1f, 1f); @@ -387,6 +400,7 @@ void HandleMouseSteer(float deltaTime) // Apply rotation transform.Rotate(0f, runtimeData.turnSpeed * deltaTime, 0f); + // Decay mouse input runtimeData.mouseInputX = Mathf.MoveTowards(runtimeData.mouseInputX, 0f, runtimeData.mouseSensitivity * deltaTime); } diff --git a/Assets/Mirror/Examples/_Common/Controllers/PlayerControllerRB/PlayerControllerRBBase.cs b/Assets/Mirror/Examples/_Common/Controllers/PlayerControllerRB/PlayerControllerRBBase.cs index 36c9ce24b..13abd3a71 100644 --- a/Assets/Mirror/Examples/_Common/Controllers/PlayerControllerRB/PlayerControllerRBBase.cs +++ b/Assets/Mirror/Examples/_Common/Controllers/PlayerControllerRB/PlayerControllerRBBase.cs @@ -125,6 +125,7 @@ public struct RuntimeData [ReadOnly, SerializeField, Range(-1f, 1f)] float _mouseInputX; [ReadOnly, SerializeField, Range(0, 30f)] float _mouseSensitivity; [ReadOnly, SerializeField] GroundState _groundState; + [ReadOnly, SerializeField] Vector2 _mousePosition; [ReadOnly, SerializeField] Vector3 _direction; [ReadOnly, SerializeField] Vector3Int _velocity; [ReadOnly, SerializeField] GameObject _controllerUI; @@ -185,6 +186,12 @@ public GroundState groundState internal set => _groundState = value; } + public Vector2 mousePosition + { + get => _mousePosition; + internal set => _mousePosition = value; + } + public Vector3 direction { get => _direction; @@ -264,6 +271,7 @@ public override void OnStartAuthority() // Calculate DPI-aware sensitivity float dpiScale = (Screen.dpi > 0) ? (Screen.dpi / BASE_DPI) : 1f; runtimeData.mouseSensitivity = turnAcceleration * dpiScale; + runtimeData.mousePosition = Input.mousePosition; SetCursor(controlOptions.HasFlag(ControlOptions.MouseSteer)); @@ -364,7 +372,7 @@ void HandleOptions() void SetCursor(bool locked) { - Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None; + //Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None; Cursor.visible = !locked; } @@ -390,8 +398,13 @@ void HandleTurning(float deltaTime) void HandleMouseSteer(float deltaTime) { + // Get mouse position, calculate delta + Vector2 currentMousePosition = Input.mousePosition; + Vector2 mouseDelta = currentMousePosition - runtimeData.mousePosition; + runtimeData.mousePosition = currentMousePosition; + // Accumulate mouse input over time - runtimeData.mouseInputX += Input.GetAxisRaw("Mouse X") * runtimeData.mouseSensitivity; + runtimeData.mouseInputX += mouseDelta.x * runtimeData.mouseSensitivity; // Clamp the accumulator to simulate key press behavior runtimeData.mouseInputX = Mathf.Clamp(runtimeData.mouseInputX, -1f, 1f); diff --git a/Assets/Mirror/Examples/_Common/Controllers/TankController/TankTurretBase.cs b/Assets/Mirror/Examples/_Common/Controllers/TankController/TankTurretBase.cs index df624e974..c8e13d98d 100644 --- a/Assets/Mirror/Examples/_Common/Controllers/TankController/TankTurretBase.cs +++ b/Assets/Mirror/Examples/_Common/Controllers/TankController/TankTurretBase.cs @@ -127,6 +127,7 @@ public struct RuntimeData [ReadOnly, SerializeField, Range(0, 30f)] float _mouseSensitivity; [ReadOnly, SerializeField] double _lastShotTime; [ReadOnly, SerializeField] GameObject _turretUI; + [ReadOnly, SerializeField] Vector2 _mousePosition; #region Properties @@ -172,6 +173,12 @@ public GameObject turretUI internal set => _turretUI = value; } + public Vector2 mousePosition + { + get => _mousePosition; + internal set => _mousePosition = value; + } + #endregion } @@ -277,6 +284,7 @@ public override void OnStartAuthority() // Calculate DPI-aware sensitivity float dpiScale = (Screen.dpi > 0) ? (Screen.dpi / BASE_DPI) : 1f; runtimeData.mouseSensitivity = turretAcceleration * dpiScale; + runtimeData.mousePosition = Input.mousePosition; SetCursor(controlOptions.HasFlag(ControlOptions.MouseLock)); this.enabled = true; @@ -316,7 +324,7 @@ void OnPlayerColorChanged(Color32 _, Color32 newColor) void SetCursor(bool locked) { - Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None; + //Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None; Cursor.visible = !locked; } @@ -356,8 +364,13 @@ void HandleTurning(float deltaTime) void HandleMouseTurret(float deltaTime) { + // Get mouse position, calculate delta + Vector2 currentMousePosition = Input.mousePosition; + Vector2 mouseDelta = currentMousePosition - runtimeData.mousePosition; + runtimeData.mousePosition = currentMousePosition; + // Accumulate mouse input over time - runtimeData.mouseInputX += Input.GetAxisRaw("Mouse X") * runtimeData.mouseSensitivity; + runtimeData.mouseInputX += mouseDelta.x * runtimeData.mouseSensitivity; // Clamp the accumulator to simulate key press behavior runtimeData.mouseInputX = Mathf.Clamp(runtimeData.mouseInputX, -1f, 1f); @@ -371,6 +384,7 @@ void HandleMouseTurret(float deltaTime) // Apply rotation turret.Rotate(0f, runtimeData.turretSpeed * deltaTime, 0f); + // Decay mouse input runtimeData.mouseInputX = Mathf.MoveTowards(runtimeData.mouseInputX, 0f, runtimeData.mouseSensitivity * deltaTime); }