Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Quaternion.LookRotation() is...wrong?.. well at least unclear

Discussion in 'Documentation' started by Polyfemos, Mar 2, 2021.

  1. Polyfemos

    Polyfemos

    Joined:
    Nov 13, 2020
    Posts:
    25
    Hi all,

    I've been struggling to get my bullets to point in the correct direction. Take a look at this;
    upload_2021-3-3_0-19-17.png
    I have used Debug.Draw to draw the new Forward direction in Blue, and the Up direction in Green.

    However, when I pass these exact same variables to the Transform.LookRotation() function, the actual rotation is not the same one (as can be seen by the gizmo in the screenshot above).
    upload_2021-3-3_0-22-35.png

    I would expect the object, and Gizmo, to actually be rotated in the calculated forward direction, but it's somehow orthogonal to it. So for some reason, LookRotation does not in fact look at the direction!?

    Now, I realize that this function has been here a long time, and I'm probably just missing something as a Unity newbie. But what?

    Help please :)

    Using Unity 2020.2
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,265
    Your up and forward vectors are position vectors, not direction vectors. You're normalizing them, but that's just making them up and forwards vectors for the origin, not your energy ball thingy. You essentially want LookRotation(newForward, transform.position.normalized):

    Code (csharp):
    1. // rotate along tangent of planet
    2. var up = transform.position.normalized;
    3. var forward = newForward.normalized;
    4.  
    5. Debug.DrawLine(transform.position, transform.position + 100f * up, Color.green);
    6. Debug.DrawLine(transform.position, transform.position + 100f * forward, Color.blue);
    7.  
    8. var lookDir = Quaternion.LookRotation(forward, up);
    9. transform.rotation = lookDir;
    I think that should do it.
     
    JoelLeeJie likes this.