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

如何平滑相机移动?Vector3.Lerp未固定时间失效问题求助

为什么你的相机旋转Lerp没生效?

嘿,这个问题我太熟了!你遇到的核心问题,就是没给Lerp加上时间增量控制,再加上对Vector3.Lerp的工作逻辑理解有点偏差,导致平滑效果完全没出来。我给你拆解清楚:

问题根源:Lerp的t参数用错了

Vector3.Lerp(a, b, t)里的t是0到1之间的插值进度:

  • 当t=0时,返回a;
  • 当t=1时,返回b;
  • 当t在0-1之间时,返回a到b之间的过渡值。

你代码里写的t=5,这已经远远超过1了,所以Lerp会直接返回目标值cameraRotation,相当于完全没做插值。而且你每帧都把起始值orgRotation设为当前帧的旋转,再加上没有时间控制,每帧的插值逻辑完全混乱,自然看不到平滑效果。

另外还有个隐藏坑:直接用eulerAngles做旋转插值很容易出现万向锁或者角度跳变(比如从350度Lerp到10度,会绕远路走340度,而不是直接走20度),这也是你可能遇到奇怪旋转的原因。

修复方案:用时间增量+四元数插值

要实现真正的平滑旋转,我们需要:

  1. 用Time.deltaTime让插值速度和帧率无关;
  2. 用四元数(Quaternion)来处理旋转,避免欧拉角的坑;
  3. 维护一个目标旋转值,而不是直接用鼠标输入的瞬时值。

给你改好的代码示例:

// 在脚本类里声明变量,用来存储目标旋转和平滑速度
private Vector3 targetCameraRotation;
public float smoothRotateSpeed = 2.5f; // 可以在Inspector里调整快慢
public float mouseSensitivity = 2f; // 鼠标灵敏度

void Update()
{
    // 1. 处理鼠标输入,累积目标旋转(这里假设鼠标Y轴控制相机X轴旋转)
    float mouseYInput = Input.GetAxis("Mouse Y");
    targetCameraRotation.x += mouseYInput * mouseSensitivity;
    
    // 可选:限制相机旋转范围,避免抬头/低头过度
    targetCameraRotation.x = Mathf.Clamp(targetCameraRotation.x, -85f, 85f);

    // 2. 用四元数做平滑插值
    Quaternion currentRot = cam.transform.rotation;
    Quaternion targetRot = Quaternion.Euler(targetCameraRotation);
    
    // 用Slerp(球面插值)比Lerp更适合旋转,Time.deltaTime保证速度稳定
    cam.transform.rotation = Quaternion.Slerp(currentRot, targetRot, smoothRotateSpeed * Time.deltaTime);
}

如果你非要用欧拉角插值(不推荐)

如果坚持用Vector3.Lerp,一定要处理角度循环问题,比如用Mathf.DeltaAngle计算正确的角度差,再基于时间插值:

void Update()
{
    float mouseYInput = Input.GetAxis("Mouse Y");
    targetCameraRotation.x += mouseYInput * mouseSensitivity;
    targetCameraRotation.x = Mathf.Clamp(targetCameraRotation.x, -85f, 85f);

    Vector3 currentEuler = cam.transform.rotation.eulerAngles;
    // 计算X轴的正确插值角度,避免绕远路
    float newX = Mathf.LerpAngle(currentEuler.x, targetCameraRotation.x, smoothRotateSpeed * Time.deltaTime);
    
    cam.transform.rotation = Quaternion.Euler(newX, currentEuler.y, currentEuler.z);
}

核心还是要记住:任何平滑过渡都要基于时间增量,不然帧率变化会导致平滑效果忽快忽慢,甚至完全失效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:15:16