【发布时间】:2018-08-16 14:50:24
【问题描述】:
我正在使用这个简单的 CameraFollow 脚本让我的相机跟随玩家的动作。问题是我的播放器可以旋转一个完整的 360 度,并且相机必须随之旋转。这很好用,除非完成一个完整的回合。当玩家变换从 359 度回到 0 时,相机会闪烁,因为它会执行一个完整的向后 360 度循环来追赶,而不是移动 1 度来追赶。我怎样才能解决这个问题?
在下面的代码中,'target' 是我的播放器,'trans' 是相机的变换。还值得注意的是,如果玩家正好停在 0 度,它会出于某种原因跳回到 180 度。
public class CameraFollow : MonoBehaviour {
[SerializeField] Transform target;
[SerializeField] Vector3 defaultDistance = new Vector3(0f, 3.5f, -12f);
[SerializeField] float distanceDamp = 0.05f;
[SerializeField] Vector3 velocity = Vector3.one;
Transform trans;
private void Awake()
{
trans = transform;
}
private void FixedUpdate()
{
Vector3 toPos = target.position + (target.rotation * defaultDistance);
Vector3 curPos = Vector3.SmoothDamp(trans.position, toPos, ref velocity, distanceDamp);
trans.position = curPos;
trans.up = target.up;
}
}
【问题讨论】:
-
您的代码只显示位置更新,不显示旋转更新。
-
旋转在最后一行更新,通过将相机 transform.up 设置为与玩家相同。
-
您可能不应该为此使用 FixedUpdate。
-
如果我使用 Update 或 LateUpdate,我的播放器会非常紧张。
-
我相信它与 FixedUpdate 没有任何关系。我相信我需要以某种方式使用四元数来让相机的旋转超过 360 度并继续旋转。这将替换方法的最后一行,但我不确定如何正确使用四元数。