Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by sophoni, Jul 3, 2022.

  1. sophoni

    sophoni

    Joined:
    Jul 19, 2015
    Posts:
    5
    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:

    Code (CSharp):
    1.      
    2.  
    3. // ROTATIONS
    4. Vector3 newUpDirection = transform.TransformDirection(direction);
    5.  
    6. // find new forward direction
    7. // get rotation axis
    8. Vector3 rotationAxis = Vector3.Cross(transform.TransformDirection(direction), transform.TransformDirection(Vector3.up));
    9. Debug.Log("Rotation Axis: " + rotationAxis);
    10.  
    11. Vector3 newForwardDirection = Quaternion.AngleAxis(-90f, rotationAxis) * newUpDirection;
    12.  
    13. var newRotation = Quaternion.LookRotation(newForwardDirection, newUpDirection);
    14. Debug.Log("New rotation: " + newRotation.eulerAngles);
    15. TargetRotation = newRotation;
    Thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    sophoni likes this.
  3. sophoni

    sophoni

    Joined:
    Jul 19, 2015
    Posts:
    5
    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?
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    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.
     
    sophoni likes this.
  5. sophoni

    sophoni

    Joined:
    Jul 19, 2015
    Posts:
    5
    Thanks for the reply,

    I have the new up direction converted to world space I think (didn't know about transform.up thanks!)

    Code (CSharp):
    1.         // Summary:
    2.         //     Transforms direction from local space to world space.
    3.         //
    4.         // Parameters:
    5.         //   direction:
    6.         public Vector3 TransformDirection(Vector3 direction);
    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
    Code (CSharp):
    1. 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.