🤔

Quaternion.FromToRotation#

Declaration#

public static Quaternion FromToRotation(Vector3 fromDirection, Vector3 toDirection);

Description#

Creates a rotation from one direction to another. This will be the most direct rotation.
When applied as an orientation, the fromDirection axis is rotated to point in toDirection.

Interactive diagram#

Drag the sphere near fromDirection or toDirection to modify their direction.

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

There is an equivalence to AngleAxis here, axis is the cross product, and angle is made between from and to.

Note that the rotation angle is scalar—though defined by fromDirection, it does not start at the visualised position.

In use#

Setting transform.right and transform.up uses FromToRotation1.

// The red axis of the transform in world space.
public Vector3 right { ... set { rotation = Quaternion.FromToRotation(Vector3.right, value); } }

// The green axis of the transform in world space.
public Vector3 up { ... set { rotation = Quaternion.FromToRotation(Vector3.up, value); } }

Return to Quaternions.