Home > Back-end >  Rotate the y in a quaternion? Work arounds?
Rotate the y in a quaternion? Work arounds?

Time:11-16

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.

CodePudding user response:

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.

  • Related