Refactor: dedupe input reads, material-property guards, button hooks
Extract byte-identical duplicated code into shared helpers (behaviour-neutral): - New InputUtil.cs: single source for mouse-delta/scroll/cursor-lock, modifier and mouse-button reads, previously copied verbatim across camera controllers, MeshPainter, FirstPersonMode and PlayerCharacterController. - GetMatFloat/SetMatFloat replace the 12x material-instance + HasProperty guard pattern for the camo/height float properties. - Route two inline button hooks through the existing Hook() helper. Bespoke variants deliberately kept local (FreeCam SprintHeld = LeftShift only, MeshPainter.TryGetMouse with out-params). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
626dcc1bdf
commit
882d30797e
11 files changed
+158
-222
No files matched your search
@@ -1,8 +1,5 @@
|
||||
using UnityEngine;
|
||||
using Unity.Cinemachine;
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
using UnityEngine.InputSystem;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// First-person camera. Drives its OWN dedicated <see cref="CinemachineCamera"/>: each frame it snaps
|
||||
@@ -90,13 +87,13 @@ public class FirstPersonController : MonoBehaviour
|
||||
if (active)
|
||||
{
|
||||
SyncToCurrentView();
|
||||
SetCursorLocked(true);
|
||||
InputUtil.SetCursorLocked(true);
|
||||
ApplyTransform(); // place at the eye on the same frame so the blend starts from there
|
||||
if (_player) _player.FaceYaw = _displayYaw; // body faces the camera from frame one
|
||||
}
|
||||
else
|
||||
{
|
||||
SetCursorLocked(false);
|
||||
InputUtil.SetCursorLocked(false);
|
||||
if (_player) _player.FaceYaw = null; // restore movement-facing
|
||||
}
|
||||
|
||||
@@ -113,7 +110,7 @@ public class FirstPersonController : MonoBehaviour
|
||||
|
||||
void LookTick()
|
||||
{
|
||||
Vector2 delta = ReadMouseDelta() * MouseSensitivity.Multiplier;
|
||||
Vector2 delta = InputUtil.ReadMouseDelta() * MouseSensitivity.Multiplier;
|
||||
_yaw += delta.x * horizontalSensitivity;
|
||||
float vs = verticalSensitivity * (invertY ? 1f : -1f);
|
||||
_pitch = Mathf.Clamp(_pitch + delta.y * vs, -maxPitch, maxPitch);
|
||||
@@ -212,19 +209,4 @@ public class FirstPersonController : MonoBehaviour
|
||||
if (hiddenRenderers == null || hiddenRenderers.Length == 0)
|
||||
hiddenRenderers = player.GetComponentsInChildren<SkinnedMeshRenderer>(true);
|
||||
}
|
||||
|
||||
Vector2 ReadMouseDelta()
|
||||
{
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
return Mouse.current != null ? Mouse.current.delta.ReadValue() : Vector2.zero;
|
||||
#else
|
||||
return new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * 10f;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void SetCursorLocked(bool locked)
|
||||
{
|
||||
Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None;
|
||||
Cursor.visible = !locked;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user