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


    Nov 13, 2020
    Hi all,

    I've been struggling to get my bullets to point in the correct direction. Take a look at this;
    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).

    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


    Jan 24, 2013
    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;
    5. Debug.DrawLine(transform.position, transform.position + 100f * up,;
    6. Debug.DrawLine(transform.position, transform.position + 100f * forward,;
    8. var lookDir = Quaternion.LookRotation(forward, up);
    9. transform.rotation = lookDir;
    I think that should do it.
    JoelLeeJie likes this.