unity3d 无法将X轴夹紧的摄像机旋转到特定目标

gwbalxhn  于 2023-01-26  发布在  其他
关注(0)|答案(1)|浏览(157)

在一个项目中,我有一个球员与夹紧头部运动围绕垂直轴[-85,85],我需要旋转头部到特定的方向,但头部一直面朝下,我做错了什么?
我使用单独的向量axisViewCamera将旋转应用于头部:
cameraBody.localRotation = Quaternion.Euler(axisViewCamera);
夹紧:axisViewCamera.x = Mathf.Clamp(axisViewCamera.x, -85f, 85f);
然后,我从Transform.forward中获取方向,或者从两个Transform中计算它,并在DOTween的帮助下像这样应用它:

var euler = Quaternion.LookRotation(direction, Vector3.up).eulerAngles;
var original = Quaternion.LookRotation(axisViewCamera, Vector3.up).eulerAngles;
blockViewChange = true;
DOTween.To(SetAxis, 0f, 1f, 1).SetTarget(playerCamera)
      .OnComplete(() => blockViewChange = false).SetUpdate(true);

void SetAxis(float t) => axisViewCamera = Vector3.Lerp(orig, euler, t);
deyfvvtc

deyfvvtc1#

经过10小时的睡眠和一些额外的谷歌搜索和调试,我发现我的axisViewCamera没有夹紧正确,这是好的,直到我试图lerp后,一些旋转适用于相机;
所以我想这种夹紧方法应该应用于axisViewCamera的每个轴,每次我改变它,就像我从方向得到的欧拉变量一样。
大声呼喊:https://stackoverflow.com/a/2323034/15787374

private float SmartClampAngle(float angle)
{
    var remainder = angle                      % 360f;
    var positiveRemainder = (remainder + 360f) % 360f;
    if (positiveRemainder > 180) positiveRemainder -= 360f;
    return positiveRemainder;
}

结果:
axisViewCamera更新:

private void UpdateCameraRotation(Vector2 viewAxis)
{
    axisViewCamera.x -= viewAxis.y;
    axisViewCamera.y += viewAxis.x;
    axisViewCamera.x = SmartClampAngle(axisViewCamera.x);  //Previously i just applied + and - without any clamp       
    axisViewCamera.y = SmartClampAngle(axisViewCamera.y);
}

方向变更:

var euler = Quaternion.LookRotation(direction, Vector3.up).eulerAngles;
euler.x = SmartClampAngle(euler.x); 
euler.y = SmartClampAngle(euler.y); 

var orig = Quaternion.LookRotation(axisViewCamera, Vector3.up).eulerAngles;
blockViewChange = true;
DOTween.To(SetAxis, 0f, 1f, 1).SetTarget(playerCamera)
      .OnComplete(() => blockViewChange = false).SetUpdate(true);

void SetAxis(float t) => axisViewCamera = Vector3.Lerp(orig, euler, t);

相关问题