You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何无法直接将Mouse Y输入传入Unity相机X轴旋转?

Unity FPS相机旋转:为啥直接赋值mouseDelta.y没效果?

核心原因:搞懂「增量值」和「绝对角度」的区别

Input.GetAxis("Mouse X")/Input.GetAxis("Mouse Y")返回的是每帧鼠标的移动增量,不是相机该转到的绝对角度。

角色Y轴旋转能用Rotate的道理

transform.Rotate(Vector3.up * mouseDelta.x)是增量旋转——每帧在当前角色角度的基础上,叠加鼠标X轴的移动增量,所以能持续累积转向,实现正常的左右转头。

直接赋值相机角度为啥没反应

你写的playerChar_FP_POV.localEulerAngles = Vector3.right * mouseDelta.y是直接设置绝对角度:

  • 每帧鼠标Y轴的delta值通常很小(比如0.1、0.5这种),直接把相机角度设成这么小的数值,肉眼根本看不出变化;
  • 下一帧新的delta又会覆盖这个值,相机角度永远停留在微小的增量上,自然看起来“无响应”。

用cameraPitch累加的正确逻辑

cameraPitch -= mouseDelta.y是把每帧的鼠标Y轴增量累积成绝对角度值,再赋值给相机:

  • 每帧的旋转是基于之前的角度持续变化,能实现平滑的俯仰;
  • Mathf.Clamp用来限制俯仰范围(-90到90度),避免FPS相机抬头/低头过度导致视角翻转。

正确代码示例

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovementScript_V3 : MonoBehaviour
{
    [SerializeField] Transform playerChar_FP_POV = null;
    [SerializeField] float mouseSensitivty = 3.5f;
    float cameraPitch = 0.0f;

    void Start(){}

    void Update()
    {
       PlayerCameraControl(); 
    }

    public void PlayerCameraControl()
    {
        Vector2 mouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));

        cameraPitch -= mouseDelta.y;
        cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);

        playerChar_FP_POV.localEulerAngles = Vector3.right * cameraPitch;
        transform.Rotate(Vector3.up * mouseDelta.x * mouseSensitivty);
    }
}

错误尝试代码示例

public void PlayerCameraControl()
{
    Vector2 mouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));

    playerChar_FP_POV.localEulerAngles = Vector3.right * mouseDelta.y;
    transform.Rotate(Vector3.up * mouseDelta.x * mouseSensitivty);
}

Unity版本:2022.3.22f1

内容的提问来源于stack exchange,提问作者HergTheFlerg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 13:54:55