gpt4 book ai didi

c# - 如何从一个方向获得 XY 欧拉角?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:31:17 27 4
gpt4 key购买 nike

我有一个第一人称相机 Controller 实现如下:

float rotSpeed = 20.0f;

xRotation += deltaPos.y * rotSpeed * Time.deltaTime;
yRotation += deltaPos.x * -rotSpeed * Time.deltaTime;

while(xRotation > 360.0f) xRotation -= 360.0f;
while(xRotation < 0.0f) xRotation += 360.0f;

while(yRotation > 360.0f) yRotation -= 360.0f;
while(yRotation < 0.0f) yRotation += 360.0f;

cam.transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);

我希望相机能够捕捉到 3D 空间中的某个目标。

如果我使用 transform.LookAt(target),相机 Controller 只会将 View 立即捕捉到它来自的位置,并且我无法在调用 LookAt() 后根据相机的旋转设置 xRotation 和 yRotation,因为LookAt() 有时会生成 Z 轴旋转的四元数。

那么我怎样才能找到将相机指向某个目标所需的 X 和 Y 旋转?

最佳答案

您可以使用 Quaternion.LookRotation 轻松修改玩家的旋转然后用 Quaternion.Lerp 插入它.

public Transform target;
float turnSpeed = 5.0f;

Quaternion lookAt = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Lerp(transform.rotation, lookAt, Time.smoothDeltaTime * turnSpeed);

其中 transform 是玩家的变换。

您在这里要做的就是确定吸附规则何时发生。

后记:实际上可能还不止这些。如果目标/玩家位置经常移动,您可能会遇到一个“死区”,在该死区中 LookRotation 无法可靠地捕捉到它的目标。有关此问题(和解决方案)的更多详细信息 here

关于c# - 如何从一个方向获得 XY 欧拉角?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11641614/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com