在四元数中旋转 y?解决方法?

Rotate the y in a quaternion? Work arounds?

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 度。我一直在摆弄一些不同的东西,但似乎不太明白。

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

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

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