【问题标题】:Rotate the y in a quaternion? Work arounds?在四元数中旋转y?解决问题?
【发布时间】:2021-12-27 05:38:06
【问题描述】:
float speed = 20.0f;
float rSpeed = 200.0f;

// Start is called before the first frame update
void Start()
{
}

// Update is called once per frame
void Update()
{
    float xValue = Input.GetAxis("Horizontal");
    float zValue = Input.GetAxis("Vertical");
    float yValue = 0;
    Vector3 movementDir = new Vector3(xValue,yValue,zValue);
    movementDir.Normalize();

    transform.Translate(movementDir * speed * Time.deltaTime, Space.World);

    if (movementDir != Vector3.zero)
    {
        Quaternion toRotation = Quaternion.LookRotation(movementDir, Vector3.up);
        transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rSpeed * Time.deltaTime);
    }

代码应允许移动并沿移动方向转动。它按预期进行,但我的对象需要在当前正在执行的操作之上旋转 90 度。我一直在摆弄一些不同的东西,但似乎无法完全理解。

【问题讨论】:

    标签: c# unity3d rotation quaternions


    【解决方案1】:

    您可以通过将它们的四元数相乘来将两个旋转相加:

    Quaternion combinedRotation = Quaternion.Euler( 0f, 90f, 0f ) * toRotation;
    

    顺序很重要,因此如果结果看起来不正确,请交换 Quaternion.EulertoRotation 的顺序。

    【讨论】:

      猜你喜欢
      • 2016-05-05
      • 1970-01-01
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      • 2023-04-03
      • 2012-08-16
      相关资源
      最近更新 更多