统一使用新输入系统
This commit is contained in:
@@ -6,12 +6,18 @@ namespace Agame
|
||||
/// <summary>
|
||||
/// 第一人称视角控制:鼠标横移带动 body 偏航,纵移带动相机俯仰。
|
||||
///
|
||||
/// 输入来自父节点(Player 根)上 PlayerInput 的 Look 动作。
|
||||
///
|
||||
/// 网络侧前提(重要):本组件挂在 PlayerCamera 上,而 PlayerCamera 只会被
|
||||
/// <see cref="PlayerController"/> 为「本地拥有者」启用(见其 ApplyOwnership)。
|
||||
/// 非拥有者客户端的相机是 disabled 的,因此这里不会执行,天然不需要再判所有权。
|
||||
///
|
||||
/// 反过来说:如果日后把 PlayerCamera 改成常驻启用,必须补上所有权判断,
|
||||
/// 否则每个客户端都会用自己的鼠标去转动别人玩家的视角。
|
||||
///
|
||||
/// 关于光标:锁定/释放光标属于「窗口」层面,不是 gameplay 输入,
|
||||
/// 所以刻意没有做成 Action —— 那样的话玩家一旦切到别的输入上下文
|
||||
/// (比如打开菜单、禁用了 Player 这张 Map),连恢复光标的按键都收不到了。
|
||||
/// </summary>
|
||||
public class MouseLook : MonoBehaviour
|
||||
{
|
||||
@@ -27,11 +33,17 @@ namespace Agame
|
||||
[Tooltip("被偏航(Horizontal)的躯体 Transform,通常是玩家根节点。")]
|
||||
[SerializeField] private Transform characterBody;
|
||||
|
||||
private InputAction _lookAction;
|
||||
private float _xRotation;
|
||||
private bool _warnedMissingBody;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
// PlayerInput 挂在父节点(Player 根)上;相机自身可能是 disabled 的,
|
||||
// 所以要 includeInactive 才能取到。
|
||||
PlayerInput playerInput = GetComponentInParent<PlayerInput>(includeInactive: true);
|
||||
_lookAction = PlayerInputNames.Resolve(playerInput, PlayerInputNames.Look, this);
|
||||
|
||||
// 跟随本对象的启用状态锁定光标:只有本地拥有者的相机会被启用,
|
||||
// 所以光标锁定/释放自然就只在拥有者这一侧发生。
|
||||
LockCursor(true);
|
||||
@@ -45,23 +57,16 @@ namespace Agame
|
||||
|
||||
private void Update()
|
||||
{
|
||||
Mouse mouse = Mouse.current;
|
||||
if (mouse == null)
|
||||
return;
|
||||
|
||||
// Esc 释放光标,左键重新抓取(方便编辑器里切出去)。
|
||||
Keyboard kb = Keyboard.current;
|
||||
if (kb != null && kb.escapeKey.wasPressedThisFrame)
|
||||
LockCursor(false);
|
||||
|
||||
if (mouse.leftButton.wasPressedThisFrame && Cursor.lockState != CursorLockMode.Locked)
|
||||
LockCursor(true);
|
||||
UpdateCursorLock();
|
||||
|
||||
if (Cursor.lockState != CursorLockMode.Locked)
|
||||
return;
|
||||
|
||||
// 新输入系统下 delta 的单位是像素,不是轴值。
|
||||
Vector2 delta = mouse.delta.ReadValue();
|
||||
if (_lookAction == null)
|
||||
return;
|
||||
|
||||
// Look 绑定的是 <Mouse>/delta,读出来的就是这一帧的鼠标位移(单位:像素)。
|
||||
Vector2 delta = _lookAction.ReadValue<Vector2>();
|
||||
|
||||
// 横移:转动躯体
|
||||
if (characterBody != null)
|
||||
@@ -81,6 +86,20 @@ namespace Agame
|
||||
transform.localRotation = Quaternion.Euler(_xRotation, 0f, 0f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Esc 释放光标,左键重新抓取(方便编辑器里切出去)。
|
||||
/// </summary>
|
||||
private void UpdateCursorLock()
|
||||
{
|
||||
Keyboard keyboard = Keyboard.current;
|
||||
if (keyboard != null && keyboard.escapeKey.wasPressedThisFrame)
|
||||
LockCursor(false);
|
||||
|
||||
Mouse mouse = Mouse.current;
|
||||
if (mouse != null && mouse.leftButton.wasPressedThisFrame && Cursor.lockState != CursorLockMode.Locked)
|
||||
LockCursor(true);
|
||||
}
|
||||
|
||||
private void LockCursor(bool locked)
|
||||
{
|
||||
Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None;
|
||||
|
||||
Reference in New Issue
Block a user