简体   繁体   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);
    }

The code should allow for movement and turning in the direction of the movement.代码应允许移动和沿移动方向转动。 It does that as intended but my object needs to rotate 90 on top of what it is currently doing.它按预期执行此操作,但我的对象需要在当前正在执行的操作之上旋转 90。 I've been fiddling around with some different things but can't seem to quite get it.我一直在摆弄一些不同的东西,但似乎不太明白。

You can add two rotations together by multiplying their Quaternions:您可以通过乘以四元数来将两个旋转相加:

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

The order matters so if the result doesn't look correct, swap the order of Quaternion.Euler and toRotation .顺序很重要,因此如果结果看起来不正确,请交换Quaternion.EulertoRotation的顺序。

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

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