Alternative to Quaternion.LookRotation() ?

I am using the following code to setup my rotations:

// Get direction of next waypoint
Vector3 direction = waypoints[currentWaypoint].position - transform.position;
// Check if we passed the current waypoint and get the next one if we did
if( transform.position.x > waypoints[currentWaypoint].position.x )
    Quaternion newRotation = Quaternion.LookRotation( direction.normalized );    // Replace?
    float yAngle = Mathf.LerpAngle( transform.eulerAngles.y,newRotation.eulerAngles.y - 90, rBody.velocity.magnitude * Time.deltaTime );
    rigidbody.MoveRotation( Quaternion.Euler( 0, yAngle, transform.eulerAngles.z ));

As you can see I’m using ‘Quaternion.LookRotation( direction.normalized )’ to calculate the rotation towards the new target, but this only works on ‘transform.forward’ and my vehicle is moving on the x axis (transform.right) instead of the z axis, which is forward.

That’s why i do ‘newRotation.eulerAngles.y - 90’ in the angle calculation. I would rather not do that, and I also don’t want to child the vehicle to a parent which faces forward, so I was wondering if there is any alternative to Quaternion.LookRotation() that i can use, which doesn’t force the rotation to the z axis?

Thanks for your time guys! Any help is greatly appreciated :slight_smile:


Found the solution, here is an alternative to using Quaternion.LookRotation():

float turnAngle = Mathf.Atan2( direction.z, direction.x ) * Mathf.Rad2Deg;
float smoothAngle = Mathf.LerpAngle( transform.eulerAngles.y, -turnAngle, rBody.velocity.magnitude * Time.deltaTime );
rigidbody.MoveRotation( Quaternion.Euler( 0, smoothAngle, transform.eulerAngles.z ));

The Mathf.Atan2 function return an angle from 2 numbers (* Mathf.Rad2Deg is necessary to convert the angle to degrees), which is all I needed! Then I just create a smooth rotation from my vehicle’s current y rotation to the new angle, and apply the results to “rigidbody.MoveRotation()”.

Only thing I had to do for it to work correctly was to use ‘-turnAngle’ instead of ‘turnAngle’ for the rotation to happen in the correct direction.

That’s it! Maybe this will be helpful to others :slight_smile:


There’s an overload for LookRotation that allows you to specify the ‘Up’ vector. Just change that line to

Quaternion newRotation = Quaternion.LookRotation( direction.normalized, transform.right);

and you should be sorted.