向相机方向旋转第三人称?
Rotation third person in direction to camera?
我希望我的对象在旋转相机时同时朝向相机旋转,以便玩家在行走时始终朝向相机的方向,就像在典型的第三人称游戏中一样。
不幸的是,到目前为止使用的任何方法都会让我旋转物体,稍微落后于相机的旋转。
我一直在寻找一个星期的解决方案,我很绝望。
我发布了一个关于物体旋转的视频。
private Rigidbody rb;
public float speed = 1.0f;
void Start()
{
rb = GetComponent<Rigidbody>();
}
direction = Camera.main.transform.forward;
direction.y = 0.00f;
private void FixedUpdate ()
{
Quaternion targetRotation = Quaternion.LookRotation(direction);
Quaternion newRotation = Quaternion.Lerp(rb.transform.rotation, targetRotation, speed * Time.fixedDeltaTime);
rb.MoveRotation(newRotation);
}
}
我认为您不需要任何这些代码。您可以将相机作为角色的子变换放置在层次结构中,并对齐它,使其位于角色的上方和后面。当他移动时,相机应该会自动跟随。
这是对 Quaternion.Lerp
的不当使用,因为您没有使用一定比例的移动距离。目前,您基本上是在告诉它只旋转目标旋转的一小部分。
您应该使用 Quaternion.RotateTowards
,因为您可以根据 deltaTime
(或者您使用的 fixedDeltaTime
)轻松计算 angular 旋转:
public float speed = 30f; // max rotation speed of 30 degrees per second
// ...
Quaternion newRotation = Quaternion.RotateTowards(rb.transform.rotation, targetRotation,
speed * Time.deltaTime);
rb.MoveRotation(newRotation);
在re-reading你的问题之后,现在看来你不想顺利transition/lerp。在那种情况下,只需将旋转设置为目标旋转即可:
rb.MoveRotation(targetRotation);
如果这还不够 interpolation/not 足够直接,您可以在 Update
中设置刚体的旋转:
private void Update ()
{
rb.rotation = Quaternion.LookRotation(direction);
}
我希望我的对象在旋转相机时同时朝向相机旋转,以便玩家在行走时始终朝向相机的方向,就像在典型的第三人称游戏中一样。 不幸的是,到目前为止使用的任何方法都会让我旋转物体,稍微落后于相机的旋转。 我一直在寻找一个星期的解决方案,我很绝望。 我发布了一个关于物体旋转的视频。
private Rigidbody rb;
public float speed = 1.0f;
void Start()
{
rb = GetComponent<Rigidbody>();
}
direction = Camera.main.transform.forward;
direction.y = 0.00f;
private void FixedUpdate ()
{
Quaternion targetRotation = Quaternion.LookRotation(direction);
Quaternion newRotation = Quaternion.Lerp(rb.transform.rotation, targetRotation, speed * Time.fixedDeltaTime);
rb.MoveRotation(newRotation);
}
}
我认为您不需要任何这些代码。您可以将相机作为角色的子变换放置在层次结构中,并对齐它,使其位于角色的上方和后面。当他移动时,相机应该会自动跟随。
这是对 Quaternion.Lerp
的不当使用,因为您没有使用一定比例的移动距离。目前,您基本上是在告诉它只旋转目标旋转的一小部分。
您应该使用 Quaternion.RotateTowards
,因为您可以根据 deltaTime
(或者您使用的 fixedDeltaTime
)轻松计算 angular 旋转:
public float speed = 30f; // max rotation speed of 30 degrees per second
// ...
Quaternion newRotation = Quaternion.RotateTowards(rb.transform.rotation, targetRotation,
speed * Time.deltaTime);
rb.MoveRotation(newRotation);
在re-reading你的问题之后,现在看来你不想顺利transition/lerp。在那种情况下,只需将旋转设置为目标旋转即可:
rb.MoveRotation(targetRotation);
如果这还不够 interpolation/not 足够直接,您可以在 Update
中设置刚体的旋转:
private void Update ()
{
rb.rotation = Quaternion.LookRotation(direction);
}