# New rotation calculated with Quaternion.LookRotation is not exactly the expected value

Hi,

I’m trying to rotate an object 90 degrees forward to its rotation axis (-1, 0, 0). I’m using the cross product to calculate the rotation axis based on the movement direction and the current Vector3.up. Then I’m calculating the new forward and up direction and then I’m using these to get the new rotation for my object with Quaternion.LookRotation. The problem however is that the new rotation result is not 100% accurate, the result is: (89.98, 0, 0) while it should have been: (90, 0, 0). Any Ideas why? My code looks a bit like this:

``````// ROTATIONS
Vector3 newUpDirection = transform.TransformDirection(direction);

// find new forward direction
// get rotation axis
Vector3 rotationAxis = Vector3.Cross(transform.TransformDirection(direction), transform.TransformDirection(Vector3.up));
Debug.Log("Rotation Axis: " + rotationAxis);

Vector3 newForwardDirection = Quaternion.AngleAxis(-90f, rotationAxis) * newUpDirection;

var newRotation = Quaternion.LookRotation(newForwardDirection, newUpDirection);
Debug.Log("New rotation: " + newRotation.eulerAngles);
TargetRotation = newRotation;
``````

Thanks!

Floating point imprecision.

https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html

1 Like

Just a note, I’m not trying to Slerp to the new rotation - the variable newRotation result is (89.98, 0, 0) instead of (90, 0, 0), is that a normal thing when using Quaternions?

First of all, that’s not the value of newRotation. That’s the value of the eulerAngles conversion of the quaternion value. Also this particular rotation you’re talking about is one of the two gimbal lock orientations that only applies to the euler angles representation. At this rotation euler angles will loose one of the three degrees of freedom. Quaternions don’t suffer from gimbal lock, but euler angles do.

Your whole code looks a bit suspicious. Why do you have your new up direction in local space?
Also this: `transform.TransformDirection(Vector3.up)` is a confoluted way to write `transform.up`

Be careful when printing out vector3 values. Unity by default rounds the numbers to one decimal place. You can pass a number format to the ToString method to specify a higher precision.

1 Like

``````        // Summary:
Just to explain what I’m trying to do - I’m trying to use arrow inputs as direction to rotate an object 90 degrees on the axis of rotation. The problem is that `Quaternion.LookRotation(newForwardDirection, newUpDirection);` is not producing a rotation exactly at 90 degree intervals (but close). The issue is fixed if I use RoundToInt but seems hacky.