Trim comments and use implicit null checks for Unity objects
Reduce comments to non-obvious "why" notes and convert UnityEngine.Object null checks to implicit bool (if (obj) / if (!obj)). Input System devices (Mouse.current, Keyboard.current) and plain arrays keep explicit != null. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
3da613e1a1
commit
f758c96dc5
8 files changed
+220
-427
No files matched your search
@@ -6,30 +6,14 @@ using UnityEngine.InputSystem;
|
||||
|
||||
/// <summary>
|
||||
/// Feeds mouse input into a <see cref="CinemachineOrbitalFollow"/> so the camera
|
||||
/// orbits around the follow target like a classic third-person camera.
|
||||
///
|
||||
/// How the orbit is driven is controlled by <see cref="LookMode"/>:
|
||||
/// * AlwaysActive - rotates with every mouse move, cursor locked/hidden.
|
||||
/// * HoldRightMouseButton - rotates only while the right mouse button is held;
|
||||
/// the cursor is free otherwise (so the left button can paint).
|
||||
/// * Disabled - no rotation, cursor free.
|
||||
///
|
||||
/// The look mode is normally set by the GameModeManager when switching modes,
|
||||
/// but can also be configured directly in the Inspector for standalone use.
|
||||
///
|
||||
/// Horizontal/vertical mouse movement drives the orbital axes (yaw/pitch); the
|
||||
/// mouse wheel zooms by changing the orbit radius. The script reads the new Input
|
||||
/// System (Mouse.current / Keyboard.current) when enabled and falls back to the
|
||||
/// legacy Input Manager otherwise. The project currently runs the new Input System only.
|
||||
///
|
||||
/// Attach this to the same GameObject as the CinemachineCamera /
|
||||
/// CinemachineOrbitalFollow (the camera rig, not the player).
|
||||
/// orbits the follow target. Mouse movement drives yaw/pitch, the wheel zooms.
|
||||
/// The orbit behaviour depends on <see cref="LookMode"/> (usually set by the
|
||||
/// GameModeManager). Attach to the camera rig (the CinemachineOrbitalFollow).
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(CinemachineOrbitalFollow))]
|
||||
[DisallowMultipleComponent]
|
||||
public class ThirdPersonOrbitInput : MonoBehaviour
|
||||
{
|
||||
/// <summary>How the camera orbit is driven.</summary>
|
||||
public enum OrbitLookMode
|
||||
{
|
||||
/// <summary>Rotate with every mouse move; cursor locked/hidden.</summary>
|
||||
@@ -41,46 +25,39 @@ public class ThirdPersonOrbitInput : MonoBehaviour
|
||||
}
|
||||
|
||||
[Header("Look Mode")]
|
||||
[Tooltip("How the camera orbit is driven. Usually set by GameModeManager:\n" +
|
||||
"AlwaysActive = rotates with every mouse move (cursor locked).\n" +
|
||||
"HoldRightMouseButton = rotates only while RMB is held (cursor free otherwise).\n" +
|
||||
"Disabled = no rotation.")]
|
||||
[Tooltip("How the camera orbit is driven (usually set by GameModeManager).")]
|
||||
[SerializeField] private OrbitLookMode lookMode = OrbitLookMode.AlwaysActive;
|
||||
|
||||
[Header("Rotation")]
|
||||
[Tooltip("Horizontal look sensitivity in degrees of yaw per pixel of mouse movement.")]
|
||||
[Tooltip("Horizontal look sensitivity (degrees of yaw per pixel).")]
|
||||
public float horizontalSensitivity = 0.2f;
|
||||
|
||||
[Tooltip("Vertical look sensitivity in degrees of pitch per pixel of mouse movement.")]
|
||||
[Tooltip("Vertical look sensitivity (degrees of pitch per pixel).")]
|
||||
public float verticalSensitivity = 0.15f;
|
||||
|
||||
[Tooltip("If ON, moving the mouse up tilts the camera down (inverted pitch).")]
|
||||
[Tooltip("Invert vertical look (mouse up tilts the camera down).")]
|
||||
public bool invertY = false;
|
||||
|
||||
[Header("Zoom")]
|
||||
[Tooltip("If ON, the mouse wheel zooms the camera in and out (changes orbit radius).")]
|
||||
[Tooltip("Mouse wheel zooms the camera in and out (changes orbit radius).")]
|
||||
public bool enableZoom = true;
|
||||
|
||||
[Tooltip("How many world units the orbit radius changes per mouse wheel notch.")]
|
||||
[Tooltip("World units the orbit radius changes per mouse wheel notch.")]
|
||||
public float zoomSpeed = 0.5f;
|
||||
|
||||
[Tooltip("Closest the camera can zoom in (orbit radius in units).")]
|
||||
[Tooltip("Closest orbit radius (zoom in).")]
|
||||
public float minRadius = 1.5f;
|
||||
|
||||
[Tooltip("Farthest the camera can zoom out (orbit radius in units).")]
|
||||
[Tooltip("Farthest orbit radius (zoom out).")]
|
||||
public float maxRadius = 8f;
|
||||
|
||||
[Header("Cursor")]
|
||||
[Tooltip("If ON, the cursor is locked and hidden while the camera is actively " +
|
||||
"rotating, and freed otherwise so you can use the mouse normally.")]
|
||||
[Tooltip("Lock and hide the cursor while the camera is actively rotating.")]
|
||||
public bool manageCursor = true;
|
||||
|
||||
CinemachineOrbitalFollow _orbit;
|
||||
|
||||
/// <summary>
|
||||
/// How the camera orbit is currently driven. Setting this also updates the
|
||||
/// cursor state immediately (e.g. when GameModeManager switches modes).
|
||||
/// </summary>
|
||||
/// <summary>Current orbit mode. Setting it also refreshes the cursor state.</summary>
|
||||
public OrbitLookMode LookMode
|
||||
{
|
||||
get => lookMode;
|
||||
@@ -90,30 +67,21 @@ public class ThirdPersonOrbitInput : MonoBehaviour
|
||||
void Awake()
|
||||
{
|
||||
_orbit = GetComponent<CinemachineOrbitalFollow>();
|
||||
if (_orbit == null)
|
||||
if (!_orbit)
|
||||
Debug.LogError("[ThirdPersonOrbitInput] No CinemachineOrbitalFollow found on this GameObject.", this);
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
ApplyCursorState();
|
||||
}
|
||||
void OnEnable() => ApplyCursorState();
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
// Make sure we never leave the cursor locked/hidden when this is turned off.
|
||||
SetCursorLocked(false);
|
||||
}
|
||||
void OnDisable() => SetCursorLocked(false);
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (_orbit == null)
|
||||
if (!_orbit)
|
||||
return;
|
||||
|
||||
bool rotating = ShouldRotate();
|
||||
|
||||
// Keep the cursor in sync every frame: in HoldRightMouseButton mode the
|
||||
// lock state follows the right mouse button.
|
||||
if (manageCursor)
|
||||
SetCursorLocked(rotating);
|
||||
|
||||
@@ -133,16 +101,13 @@ public class ThirdPersonOrbitInput : MonoBehaviour
|
||||
float scroll = ReadScroll();
|
||||
if (!Mathf.Approximately(scroll, 0f))
|
||||
{
|
||||
// One wheel notch = one zoom step. Use only the sign so the step
|
||||
// size is exactly zoomSpeed regardless of the platform's raw wheel
|
||||
// magnitude (the new Input System reports ~1 per notch, the legacy
|
||||
// Windows path used ~120). Scrolling up (positive) zooms in.
|
||||
// One wheel notch = one zoom step (sign only), independent of the
|
||||
// platform's raw wheel magnitude. Scrolling up zooms in.
|
||||
_orbit.Radius = Mathf.Clamp(_orbit.Radius - Mathf.Sign(scroll) * zoomSpeed, minRadius, maxRadius);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Whether the camera should orbit this frame, based on the look mode.</summary>
|
||||
bool ShouldRotate()
|
||||
{
|
||||
switch (lookMode)
|
||||
@@ -159,10 +124,7 @@ public class ThirdPersonOrbitInput : MonoBehaviour
|
||||
SetCursorLocked(ShouldRotate());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds <paramref name="amount"/> to an input axis, respecting its range:
|
||||
/// wrapping axes (e.g. yaw) wrap around, non-wrapping axes (e.g. pitch) clamp.
|
||||
/// </summary>
|
||||
/// <summary>Adds to an input axis; wrapping axes wrap, others clamp to their range.</summary>
|
||||
static void ApplyAxisDelta(ref InputAxis axis, float amount)
|
||||
{
|
||||
float value = axis.Value + amount;
|
||||
@@ -200,7 +162,6 @@ public class ThirdPersonOrbitInput : MonoBehaviour
|
||||
Vector2 ReadMouseDelta()
|
||||
{
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
// Mouse.current.delta is already the per-frame pixel movement (no deltaTime).
|
||||
return Mouse.current != null ? Mouse.current.delta.ReadValue() : Vector2.zero;
|
||||
#else
|
||||
return new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * 10f;
|
||||
@@ -210,8 +171,7 @@ public class ThirdPersonOrbitInput : MonoBehaviour
|
||||
float ReadScroll()
|
||||
{
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
// Raw wheel delta. Only its sign is used in Update (one notch = one zoom
|
||||
// step), so the platform-dependent magnitude does not matter here.
|
||||
// Raw wheel delta; only its sign is used (one notch = one zoom step).
|
||||
return Mouse.current != null ? Mouse.current.scroll.ReadValue().y : 0f;
|
||||
#else
|
||||
return Input.GetAxis("Mouse ScrollWheel");
|
||||
|
||||
Reference in new issue
Block a user