🤔 Unity, huh, how?

🤔

Quaternion.LookRotation

Declaration

public static Quaternion LookRotation(Vector3 forward, Vector3 up = Vector3.up);

Description

Creates a rotation with forward and upwards.
upwards is used to guide the orientation, and right becomes the cross-product between it and forward.

Combined with Vector3.Cross this function is a staple for creating orientations.

Interactive diagram

Drag the sphere near forward or upwards to modify their direction.

Quaternion.LookRotation(new Vector3(0, 1, 0), new Vector3(1, 0, 0));

In use

When setting transform.forward, LookRotation is used1:

// The blue axis of the transform in world space.
public Vector3 forward { ... set { rotation = Quaternion.LookRotation(value); } }

A custom LookAt could choose to use it:

public static Quaternion GetLookAtOrientation(Vector3 origin, Vector3 target)
    => GetLookAtOrientation(origin, target, Vector3.up);

public static Quaternion GetLookAtOrientation(Vector3 origin, Vector3 target, Vector3 up)
{
    Vector3 lookDirection = target - origin;
    return Quaternion.LookRotation(lookDirection, up);
}

Return to Quaternions.
Next, Quaternion.FromToRotation.