Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading