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

Godot相机锁定功能特定角度翻转故障求助

相机锁定特定角度时翻转故障的解决方案

问题根源

你的问题出在look_at()返回的旋转角度会出现180度跳变。比如当目标绕到相机后方时,rotation_degrees.y可能从179°突然跳到-181°,而lerp()函数会直接在这两个值之间线性插值,导致相机瞬间翻转。

修复方案

需要对目标角度和当前角度做角度环绕处理,确保两者处于同一区间(比如-180°到180°),并计算最短的旋转路径,避免跨180°的跳变。

步骤1:添加角度归一化函数

在脚本中新增一个函数,用于将角度归一到-180°到180°区间:

func normalize_angle(angle: float) -> float:
    return fmod(angle + 180.0, 360.0) - 180.0

步骤2:修正目标角度获取逻辑

不再直接赋值yaw和pitch,而是先归一化角度,再计算最短差值:

if cam_locked == "target":
    $ImaginaryCam.look_at(target.position, Vector3.UP)
    # 归一化目标角度
    var target_yaw = normalize_angle($ImaginaryCam.rotation_degrees.y)
    var target_pitch = normalize_angle($ImaginaryCam.rotation_degrees.x)
    
    # 计算当前角度与目标角度的最短差值
    var yaw_diff = normalize_angle(target_yaw - yaw_node.rotation_degrees.y)
    var pitch_diff = normalize_angle(target_pitch - pitch_node.rotation_degrees.x)
    
    # 更新目标yaw和pitch为当前角度加最短差值
    yaw = yaw_node.rotation_degrees.y + yaw_diff
    pitch = pitch_node.rotation_degrees.x + pitch_diff

步骤3:保持平滑插值逻辑不变

原有的lerp代码可以保留,现在插值的目标角度已经是基于当前角度的最短路径,不会出现跳变:

yaw_node.rotation_degrees.y = lerp(yaw_node.rotation_degrees.y, yaw, yaw_acceleration * delta)
pitch_node.rotation_degrees.x = lerp(pitch_node.rotation_degrees.x, pitch, pitch_acceleration * delta)

额外提示

  • 也可以直接使用Godot内置的wrapf()函数替代自定义的归一化函数,效果一致:wrapf(angle, -180.0, 180.0)
  • 对于pitch角度,建议额外限制范围(比如-80°到80°),避免相机翻转到玩家头顶/脚下的极端情况

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 13:08:16