繁体   English   中英

在四元数中旋转 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的顺序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM