fix(Controllers): Use Input.mousePosition instead of GetAxisRaw

This commit is contained in:
MrGadget 2024-11-03 06:30:54 -05:00
parent 04bb95311b
commit c475ef95f0
10 changed files with 68 additions and 8 deletions

View File

@ -469,6 +469,7 @@ MonoBehaviour:
_mouseInputX: 0 _mouseInputX: 0
_mouseSensitivity: 0 _mouseSensitivity: 0
_groundState: 0 _groundState: 0
_mousePosition: {x: 0, y: 0}
_direction: {x: 0, y: 0, z: 0} _direction: {x: 0, y: 0, z: 0}
_velocity: {x: 0, y: 0, z: 0} _velocity: {x: 0, y: 0, z: 0}
_controllerUI: {fileID: 0} _controllerUI: {fileID: 0}

View File

@ -469,6 +469,7 @@ MonoBehaviour:
_mouseInputX: 0 _mouseInputX: 0
_mouseSensitivity: 0 _mouseSensitivity: 0
_groundState: 0 _groundState: 0
_mousePosition: {x: 0, y: 0}
_direction: {x: 0, y: 0, z: 0} _direction: {x: 0, y: 0, z: 0}
_velocity: {x: 0, y: 0, z: 0} _velocity: {x: 0, y: 0, z: 0}
_controllerUI: {fileID: 0} _controllerUI: {fileID: 0}

View File

@ -487,6 +487,7 @@ MonoBehaviour:
_mouseInputX: 0 _mouseInputX: 0
_mouseSensitivity: 0 _mouseSensitivity: 0
_groundState: 0 _groundState: 0
_mousePosition: {x: 0, y: 0}
_direction: {x: 0, y: 0, z: 0} _direction: {x: 0, y: 0, z: 0}
_velocity: {x: 0, y: 0, z: 0} _velocity: {x: 0, y: 0, z: 0}
_controllerUI: {fileID: 0} _controllerUI: {fileID: 0}

View File

@ -487,6 +487,7 @@ MonoBehaviour:
_mouseInputX: 0 _mouseInputX: 0
_mouseSensitivity: 0 _mouseSensitivity: 0
_groundState: 0 _groundState: 0
_mousePosition: {x: 0, y: 0}
_direction: {x: 0, y: 0, z: 0} _direction: {x: 0, y: 0, z: 0}
_velocity: {x: 0, y: 0, z: 0} _velocity: {x: 0, y: 0, z: 0}
_controllerUI: {fileID: 0} _controllerUI: {fileID: 0}

View File

@ -260,6 +260,7 @@ MonoBehaviour:
_mouseSensitivity: 10 _mouseSensitivity: 10
_lastShotTime: 0 _lastShotTime: 0
_turretUI: {fileID: 0} _turretUI: {fileID: 0}
_mousePosition: {x: 0, y: 0}
turretNetworkTransform: {fileID: 8662002890669114756} turretNetworkTransform: {fileID: 8662002890669114756}
barrelNetworkTransform: {fileID: 4362334322449175123} barrelNetworkTransform: {fileID: 4362334322449175123}
--- !u!114 &8662002890669114756 --- !u!114 &8662002890669114756

View File

@ -260,6 +260,7 @@ MonoBehaviour:
_mouseSensitivity: 10 _mouseSensitivity: 10
_lastShotTime: 0 _lastShotTime: 0
_turretUI: {fileID: 0} _turretUI: {fileID: 0}
_mousePosition: {x: 0, y: 0}
turretNetworkTransform: {fileID: 8518206564796711397} turretNetworkTransform: {fileID: 8518206564796711397}
barrelNetworkTransform: {fileID: 470423508858832317} barrelNetworkTransform: {fileID: 470423508858832317}
--- !u!114 &8518206564796711397 --- !u!114 &8518206564796711397

View File

@ -161,6 +161,7 @@ public struct RuntimeData
[ReadOnly, SerializeField, Range(-1f, 1f)] float _mouseInputX; [ReadOnly, SerializeField, Range(-1f, 1f)] float _mouseInputX;
[ReadOnly, SerializeField, Range(0, 30f)] float _mouseSensitivity; [ReadOnly, SerializeField, Range(0, 30f)] float _mouseSensitivity;
[ReadOnly, SerializeField] GroundState _groundState; [ReadOnly, SerializeField] GroundState _groundState;
[ReadOnly, SerializeField] Vector2 _mousePosition;
[ReadOnly, SerializeField] Vector3 _direction; [ReadOnly, SerializeField] Vector3 _direction;
[ReadOnly, SerializeField] Vector3Int _velocity; [ReadOnly, SerializeField] Vector3Int _velocity;
[ReadOnly, SerializeField] GameObject _controllerUI; [ReadOnly, SerializeField] GameObject _controllerUI;
@ -239,6 +240,12 @@ public GroundState groundState
internal set => _groundState = value; internal set => _groundState = value;
} }
public Vector2 mousePosition
{
get => _mousePosition;
internal set => _mousePosition = value;
}
public Vector3 direction public Vector3 direction
{ {
get => _direction; get => _direction;
@ -313,6 +320,7 @@ public override void OnStartAuthority()
// Calculate DPI-aware sensitivity // Calculate DPI-aware sensitivity
float dpiScale = (Screen.dpi > 0) ? (Screen.dpi / BASE_DPI) : 1f; float dpiScale = (Screen.dpi > 0) ? (Screen.dpi / BASE_DPI) : 1f;
runtimeData.mouseSensitivity = turnAcceleration * dpiScale; runtimeData.mouseSensitivity = turnAcceleration * dpiScale;
runtimeData.mousePosition = Input.mousePosition;
SetCursor(controlOptions.HasFlag(ControlOptions.MouseSteer)); SetCursor(controlOptions.HasFlag(ControlOptions.MouseSteer));
@ -392,7 +400,7 @@ void Update()
void SetCursor(bool locked) void SetCursor(bool locked)
{ {
Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None; //Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None;
Cursor.visible = !locked; Cursor.visible = !locked;
} }
@ -436,8 +444,13 @@ void HandleTurning(float deltaTime)
void HandleMouseSteer(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 // 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 // Clamp the accumulator to simulate key press behavior
runtimeData.mouseInputX = Mathf.Clamp(runtimeData.mouseInputX, -1f, 1f); runtimeData.mouseInputX = Mathf.Clamp(runtimeData.mouseInputX, -1f, 1f);

View File

@ -125,6 +125,7 @@ public struct RuntimeData
[ReadOnly, SerializeField, Range(-1f, 1f)] float _mouseInputX; [ReadOnly, SerializeField, Range(-1f, 1f)] float _mouseInputX;
[ReadOnly, SerializeField, Range(0, 30f)] float _mouseSensitivity; [ReadOnly, SerializeField, Range(0, 30f)] float _mouseSensitivity;
[ReadOnly, SerializeField] GroundState _groundState; [ReadOnly, SerializeField] GroundState _groundState;
[ReadOnly, SerializeField] Vector2 _mousePosition;
[ReadOnly, SerializeField] Vector3 _direction; [ReadOnly, SerializeField] Vector3 _direction;
[ReadOnly, SerializeField] Vector3Int _velocity; [ReadOnly, SerializeField] Vector3Int _velocity;
[ReadOnly, SerializeField] GameObject _controllerUI; [ReadOnly, SerializeField] GameObject _controllerUI;
@ -185,6 +186,12 @@ public GroundState groundState
internal set => _groundState = value; internal set => _groundState = value;
} }
public Vector2 mousePosition
{
get => _mousePosition;
internal set => _mousePosition = value;
}
public Vector3 direction public Vector3 direction
{ {
get => _direction; get => _direction;
@ -260,6 +267,7 @@ public override void OnStartAuthority()
// Calculate DPI-aware sensitivity // Calculate DPI-aware sensitivity
float dpiScale = (Screen.dpi > 0) ? (Screen.dpi / BASE_DPI) : 1f; float dpiScale = (Screen.dpi > 0) ? (Screen.dpi / BASE_DPI) : 1f;
runtimeData.mouseSensitivity = turnAcceleration * dpiScale; runtimeData.mouseSensitivity = turnAcceleration * dpiScale;
runtimeData.mousePosition = Input.mousePosition;
SetCursor(controlOptions.HasFlag(ControlOptions.MouseSteer)); SetCursor(controlOptions.HasFlag(ControlOptions.MouseSteer));
@ -327,7 +335,7 @@ void Update()
void SetCursor(bool locked) void SetCursor(bool locked)
{ {
Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None; //Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None;
Cursor.visible = !locked; Cursor.visible = !locked;
} }
@ -372,8 +380,13 @@ void HandleTurning(float deltaTime)
void HandleMouseSteer(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 // 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 // Clamp the accumulator to simulate key press behavior
runtimeData.mouseInputX = Mathf.Clamp(runtimeData.mouseInputX, -1f, 1f); runtimeData.mouseInputX = Mathf.Clamp(runtimeData.mouseInputX, -1f, 1f);
@ -387,6 +400,7 @@ void HandleMouseSteer(float deltaTime)
// Apply rotation // Apply rotation
transform.Rotate(0f, runtimeData.turnSpeed * deltaTime, 0f); transform.Rotate(0f, runtimeData.turnSpeed * deltaTime, 0f);
// Decay mouse input
runtimeData.mouseInputX = Mathf.MoveTowards(runtimeData.mouseInputX, 0f, runtimeData.mouseSensitivity * deltaTime); runtimeData.mouseInputX = Mathf.MoveTowards(runtimeData.mouseInputX, 0f, runtimeData.mouseSensitivity * deltaTime);
} }

View File

@ -125,6 +125,7 @@ public struct RuntimeData
[ReadOnly, SerializeField, Range(-1f, 1f)] float _mouseInputX; [ReadOnly, SerializeField, Range(-1f, 1f)] float _mouseInputX;
[ReadOnly, SerializeField, Range(0, 30f)] float _mouseSensitivity; [ReadOnly, SerializeField, Range(0, 30f)] float _mouseSensitivity;
[ReadOnly, SerializeField] GroundState _groundState; [ReadOnly, SerializeField] GroundState _groundState;
[ReadOnly, SerializeField] Vector2 _mousePosition;
[ReadOnly, SerializeField] Vector3 _direction; [ReadOnly, SerializeField] Vector3 _direction;
[ReadOnly, SerializeField] Vector3Int _velocity; [ReadOnly, SerializeField] Vector3Int _velocity;
[ReadOnly, SerializeField] GameObject _controllerUI; [ReadOnly, SerializeField] GameObject _controllerUI;
@ -185,6 +186,12 @@ public GroundState groundState
internal set => _groundState = value; internal set => _groundState = value;
} }
public Vector2 mousePosition
{
get => _mousePosition;
internal set => _mousePosition = value;
}
public Vector3 direction public Vector3 direction
{ {
get => _direction; get => _direction;
@ -264,6 +271,7 @@ public override void OnStartAuthority()
// Calculate DPI-aware sensitivity // Calculate DPI-aware sensitivity
float dpiScale = (Screen.dpi > 0) ? (Screen.dpi / BASE_DPI) : 1f; float dpiScale = (Screen.dpi > 0) ? (Screen.dpi / BASE_DPI) : 1f;
runtimeData.mouseSensitivity = turnAcceleration * dpiScale; runtimeData.mouseSensitivity = turnAcceleration * dpiScale;
runtimeData.mousePosition = Input.mousePosition;
SetCursor(controlOptions.HasFlag(ControlOptions.MouseSteer)); SetCursor(controlOptions.HasFlag(ControlOptions.MouseSteer));
@ -364,7 +372,7 @@ void HandleOptions()
void SetCursor(bool locked) void SetCursor(bool locked)
{ {
Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None; //Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None;
Cursor.visible = !locked; Cursor.visible = !locked;
} }
@ -390,8 +398,13 @@ void HandleTurning(float deltaTime)
void HandleMouseSteer(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 // 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 // Clamp the accumulator to simulate key press behavior
runtimeData.mouseInputX = Mathf.Clamp(runtimeData.mouseInputX, -1f, 1f); runtimeData.mouseInputX = Mathf.Clamp(runtimeData.mouseInputX, -1f, 1f);

View File

@ -127,6 +127,7 @@ public struct RuntimeData
[ReadOnly, SerializeField, Range(0, 30f)] float _mouseSensitivity; [ReadOnly, SerializeField, Range(0, 30f)] float _mouseSensitivity;
[ReadOnly, SerializeField] double _lastShotTime; [ReadOnly, SerializeField] double _lastShotTime;
[ReadOnly, SerializeField] GameObject _turretUI; [ReadOnly, SerializeField] GameObject _turretUI;
[ReadOnly, SerializeField] Vector2 _mousePosition;
#region Properties #region Properties
@ -172,6 +173,12 @@ public GameObject turretUI
internal set => _turretUI = value; internal set => _turretUI = value;
} }
public Vector2 mousePosition
{
get => _mousePosition;
internal set => _mousePosition = value;
}
#endregion #endregion
} }
@ -277,6 +284,7 @@ public override void OnStartAuthority()
// Calculate DPI-aware sensitivity // Calculate DPI-aware sensitivity
float dpiScale = (Screen.dpi > 0) ? (Screen.dpi / BASE_DPI) : 1f; float dpiScale = (Screen.dpi > 0) ? (Screen.dpi / BASE_DPI) : 1f;
runtimeData.mouseSensitivity = turretAcceleration * dpiScale; runtimeData.mouseSensitivity = turretAcceleration * dpiScale;
runtimeData.mousePosition = Input.mousePosition;
SetCursor(controlOptions.HasFlag(ControlOptions.MouseLock)); SetCursor(controlOptions.HasFlag(ControlOptions.MouseLock));
this.enabled = true; this.enabled = true;
@ -316,7 +324,7 @@ void OnPlayerColorChanged(Color32 _, Color32 newColor)
void SetCursor(bool locked) void SetCursor(bool locked)
{ {
Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None; //Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None;
Cursor.visible = !locked; Cursor.visible = !locked;
} }
@ -356,8 +364,13 @@ void HandleTurning(float deltaTime)
void HandleMouseTurret(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 // 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 // Clamp the accumulator to simulate key press behavior
runtimeData.mouseInputX = Mathf.Clamp(runtimeData.mouseInputX, -1f, 1f); runtimeData.mouseInputX = Mathf.Clamp(runtimeData.mouseInputX, -1f, 1f);
@ -371,6 +384,7 @@ void HandleMouseTurret(float deltaTime)
// Apply rotation // Apply rotation
turret.Rotate(0f, runtimeData.turretSpeed * deltaTime, 0f); turret.Rotate(0f, runtimeData.turretSpeed * deltaTime, 0f);
// Decay mouse input
runtimeData.mouseInputX = Mathf.MoveTowards(runtimeData.mouseInputX, 0f, runtimeData.mouseSensitivity * deltaTime); runtimeData.mouseInputX = Mathf.MoveTowards(runtimeData.mouseInputX, 0f, runtimeData.mouseSensitivity * deltaTime);
} }