From 5360e988fa7c47da8d58ee3a25a7ed92fd700a57 Mon Sep 17 00:00:00 2001
From: Weicao-CatilGrass <1992414357@qq.com>
Date: Fri, 22 May 2026 12:40:52 +0800
Subject: 让DeepSeek添加详尽的注释
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Assets/Scripts/InputHandler.cs | 74 +++++++++++++++++++++++++++++++++++-------
1 file changed, 63 insertions(+), 11 deletions(-)
(limited to 'Assets/Scripts/InputHandler.cs')
diff --git a/Assets/Scripts/InputHandler.cs b/Assets/Scripts/InputHandler.cs
index f653adb..4d0bba1 100644
--- a/Assets/Scripts/InputHandler.cs
+++ b/Assets/Scripts/InputHandler.cs
@@ -5,68 +5,120 @@ using UnityEngine;
namespace DS
{
+ ///
+ /// 输入处理器 - 负责处理玩家输入(移动、视角控制)并将输入数据分发给其他系统(如相机和角色控制器)。
+ ///
public class InputHandler : MonoBehaviour
{
+ // ===== 公共输入数据(供其他系统读取) =====
+ [Header("移动输入")]
+ [Tooltip("水平移动输入值 (-1~1),负值表示左,正值表示右")]
public float horizontal;
+
+ [Tooltip("垂直移动输入值 (-1~1),负值表示下,正值表示上")]
public float vertical;
+
+ [Tooltip("综合移动量 (0~1),水平与垂直绝对值的和,并被限制在 0~1 之间")]
public float moveAmount;
+
+ [Header("视角输入")]
+ [Tooltip("鼠标或右摇杆的水平移动增量")]
public float mouseX;
+
+ [Tooltip("鼠标或右摇杆的垂直移动增量")]
public float mouseY;
- private PlayerControls _inputActions;
- private CameraHandler _cameraHandler;
+ // ===== 私有引用与输入缓存 =====
+ private PlayerControls _inputActions; // 新输入系统生成的 PlayerControls 实例
+ private CameraHandler _cameraHandler; // 相机处理器单例引用
- private Vector2 _movementInput;
- private Vector2 _cameraInput;
+ private Vector2 _movementInput; // 缓存移动输入值 (x => 水平, y => 垂直)
+ private Vector2 _cameraInput; // 缓存视角输入值 (x => 水平视角, y => 垂直视角)
+
+ // ===== Unity 生命周期方法 =====
private void Awake()
{
+ // 获取相机处理器的单例引用,用于在 FixedUpdate 中驱动相机跟随与旋转
_cameraHandler = CameraHandler.singleton;
}
+ ///
+ /// 固定频率更新(用于物理相关的相机跟随和旋转)。
+ ///
private void FixedUpdate()
{
- float delta = Time.fixedDeltaTime;
+ float delta = Time.fixedDeltaTime; // 固定时间步长,确保物理一致性
if (_cameraHandler != null)
{
+ // 让相机跟随目标(通常是玩家角色)
_cameraHandler.FollowTarget(delta);
- _cameraHandler.HandleCameraRotation(delta,mouseX,mouseY);
+ // 根据鼠标/摇杆输入旋转相机
+ _cameraHandler.HandleCameraRotation(delta, mouseX, mouseY);
}
}
+ ///
+ /// 在脚本启用时注册输入事件并启用输入系统。
+ ///
public void OnEnable()
{
if (_inputActions == null)
{
+ // 实例化输入操作,并绑定事件回调
_inputActions = new PlayerControls();
+
+ // 当移动按键被触发(按下/推摇杆)时,将输入值存入 _movementInput
_inputActions.PlayerMovement.Movement.performed +=
ctx => _movementInput = ctx.ReadValue();
- _inputActions.PlayerMovement.Camera.performed += ctx => _cameraInput = ctx.ReadValue();
+
+ // 当视角控制被触发(鼠标移动/右摇杆)时,将输入值存入 _cameraInput
+ _inputActions.PlayerMovement.Camera.performed +=
+ ctx => _cameraInput = ctx.ReadValue();
}
-
+
+ // 启用输入动作监听
_inputActions.Enable();
}
+ ///
+ /// 在脚本禁用时禁用输入系统,避免误触。
+ ///
private void OnDisable()
{
_inputActions.Disable();
}
+ // ===== 公共更新入口 =====
+
+ ///
+ /// 每帧调用的输入处理入口(通常由 GameManager 或其他管理器调用)。
+ ///
+ /// 当前帧的时间增量
public void TickInput(float delta)
{
MoveInput(delta);
}
+ // ===== 私有输入处理逻辑 =====
+
+ ///
+ /// 从缓存的输入向量中提取各个方向的分量,并计算综合移动量。
+ ///
+ /// 当前帧的时间增量(目前未使用,保留以准备将来可能的平滑处理)
private void MoveInput(float delta)
{
+ // 从缓存中提取水平和垂直移动分量
horizontal = _movementInput.x;
vertical = _movementInput.y;
- moveAmount = Mathf.Clamp01(Mathf.Abs((horizontal)) + Mathf.Abs(vertical));
+
+ // 综合移动量 = 水平与垂直绝对值的和,限制在 0~1,用于控制移动动画混合等
+ moveAmount = Mathf.Clamp01(Mathf.Abs(horizontal) + Mathf.Abs(vertical));
+
+ // 从缓存中提取视角控制分量
mouseX = _cameraInput.x;
mouseY = _cameraInput.y;
-
}
}
}
-
--
cgit