简体   繁体   中英

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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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