Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Get's the rotation needed to face the specified direction.
- /// </summary>
- /// <param name="direction">The direction to face.</param>
- /// <param name="planeNormal">Projects the rotation on this normal vector.</param>
- /// <returns></returns>
- public static Quaternion LookRotation(Vector3 direction, Vector3 planeNormal)
- {
- // Calculate the up rotation.
- var upRotation = Quaternion.FromToRotation(Vector3.WorldUp, planeNormal);
- // Get the signed angle from (0, 1, 0) to the direction delta.
- var sin = Vector3.SignedAngle(Vector3.RelativeFront, direction, planeNormal);
- // Calculate the facing rotation.
- var forwardRotation = Quaternion.Euler(0, 0, sin);
- // Get the look rotation by multiplying both upQ and facingQ.
- var lookRotation = upRotation * forwardRotation;
- // Return the new rotation.
- return lookRotation;
- }
- /// <summary>
- /// Get's the rotation needed to face the specified direction.
- /// Default plane normal is our up vector.
- /// </summary>
- /// <param name="direction">The direction to face.</param>
- /// <returns></returns>
- public static Quaternion LookRotation(Vector3 direction)
- {
- return LookRotation(direction, Vector3.WorldUp);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement