5
\$\begingroup\$

I've gone through Unity's documentation for Quaternion.LookRotation, but I didn't get the full understanding of how it works.

  1. What is the meaning of Creates a rotation with the specified forward and upwards directions?
  2. And if I have to rotate my object in X axis I have to modify both X and Z axisVector3 lookAt = new Vector3(relativePos.x, 0, relativePos.z); transform.rotation = Quaternion.LookRotation(lookAt);, why can't I only just modify the X axis directly to rotate?
\$\endgroup\$
1
  • 1
    \$\begingroup\$ In that second example, it looks like you're rotating about the y axis, no? Remember the axis of rotation is perpendicular to the plane of rotation. \$\endgroup\$
    – DMGregory
    May 28, 2018 at 12:28

2 Answers 2

5
\$\begingroup\$

Quaternion.LookRotation(forward, up) constructs an orientation quaternion that...

  • rotates an object's local z+ axis (0, 0, 1) — the blue arrow on its local translation gizmo — to point exactly in the worldspace direction you pass as forward. It makes the object "look" in that direction.

  • rotates an object's local y+ axis (0, 1, 0) — the green arrow on its local translation gizmo — to point as close as possible to the worldspace direction you pass as up (or to world up if you skip this parameter). This controls the twist or roll about that forward axis.

By specifying a forward direction of the form (x, 0, z), you're ensuring that the resulting rotation will look along the XZ plane. ie. it will be a yaw rotation about the y axis, with no pitch / x-axis rotation upward or downward.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you for explaining the second point in an accessible language, until now it remained unclear to me from the official documentation. \$\endgroup\$
    – Serg
    Jun 18, 2022 at 14:03
3
\$\begingroup\$

I am not sure what you mean with "modify the X axis". If you intend to do transform.rotation.x = value, then you are going to fail, because quaternions are not that simple. There is a reason why the documentation says "Don't modify this directly unless you know quaternions inside out.". You should only modify the individual fields of a Quaternion when you actually understand the math behind them. And very few developers actually understand it, so don't feel bad when your head starts to hurt. Just accept that quaternions are magic and get on with your life.

Or maybe you are thinking about rotating a Quaternion with transform.rotation *= Quaternion.Euler(x, 0.0f, 0.0f);. Now you are on the right track. But when you want one object to rotate so that it faces a certain point, then you have to calculate the values for x yourself. And if your knowledge of trigonometry isn't the freshest, you might have to think a few minutes about how to calculate that correctly.

That's why there is a convenient method just for this common use-case.

If you just want one object to rotate in a way that it faces a different game object, you use Quaternion.LookRotation(position);. Now you might wonder why you need the additional "upwards" vector. Note that when you are looking from the perspective of the object, then there are three ways to rotate it:

  • pitch
  • yaw
  • roll

You usually want to change only pitch and yaw and keep the roll the way it is. But sometimes in order to do that you need to tell the engine the difference between pitching, yawing and rolling. That's what the upward-vector is for. Note that this is an optional argument. You can omit it. The engine will then assume you mean upwards in world-space. But sometimes you want to use a different upward-axis. The upward parameter allows you to do that if you want to.

For a possible use-case, consider a space ship with turrets attached on the top and on the sides. You want all those turrets to be able to track enemy fighters in a hemisphere oriented relative to their mounting-point. The best way to do that is obviously by using Quaternion.LookRotation(enemy.transform.position - turret.transform.position). But you still want all the guns to stay level relative to the hull of the space ship and not rotate around the barrel-axis. This is additionally complicated by the fact that the ship itself is able to rotate. So you need to tell LookRotation which way is up for that specific turret.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .