Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


Quaternion

Quaternions are used to represent rotations.
Read time 3 minutesLast updated 4 days ago

Definition

public struct Quaternion : IEquatable<Quaternion>, IFormattable

Remarks

A quaternion is a four-tuple of real numbers {x,y,z,w}. A quaternion is a mathematically convenient alternative to the euler angle representation. You can interpolate a quaternion without experiencing gimbal lock. You can also use a quaternion to concatenate a series of rotations into a single representation.
Unity internally uses Quaternions to represent all rotations.
In most cases, you can use existing rotations from methods such as Transform.localRotation or Transform.rotation to construct new rotations. For example, use existing rotations to smoothly interpolate between two rotations. The most used Quaternion functions are as follows: Quaternion.LookRotation, Quaternion.Angle, Quaternion.Euler, Quaternion.Slerp, Quaternion.FromToRotation, and Quaternion.identity.
You can use the Quaternion.operator * to rotate one rotation by another, or to rotate a vector by a rotation.
Note that Unity expects Quaternions to be normalized.

Fields

Value

Description

wW component of the Quaternion. Do not directly modify quaternions.
xX component of the Quaternion. Don't modify this directly unless you know quaternions inside out.
yY component of the Quaternion. Don't modify this directly unless you know quaternions inside out.
zZ component of the Quaternion. Don't modify this directly unless you know quaternions inside out.

Constructors

Constructor

Description

Quaternion(System.Single,System.Single,System.Single,System.Single)Constructs new Quaternion with given x,y,z,w components.

Properties

Property

Description

eulerAnglesReturns or sets the euler angle representation of the rotation in degrees.
this[]Access the x, y, z, w components using [0], [1], [2], [3] respectively.
normalizedReturns this quaternion with a magnitude of 1 (Read Only).

Static Properties

Property

Description

identityThe identity rotation (Read Only).

Methods

Method

Description

EqualsPerforms exact binary comparison of each Quaternion component (x, y, z, w).
NormalizeConverts this quaternion to a quaternion with the same orientation but with a magnitude of 1.0.
SetSet x, y, z and w components of an existing Quaternion.
SetFromToRotationCreates a rotation which rotates from
fromDirection
to
toDirection
.
SetLookRotationCreates a rotation with the specified
forward
and
upwards
directions.
ToAngleAxisConverts a rotation to angle-axis representation.
ToStringReturns a formatted string for this quaternion.

Static Methods

Method

Description

AngleReturns the angle in degrees between two rotations
a
and
b
. The resulting angle ranges from 0 to 180.
AngleAxisCreates a rotation which rotates
angle
degrees around
axis
.
DotThe dot product between two rotations.
EulerConverts an input Euler angle rotation specified as three floats to a Quaternion.
FromToRotationCreates a rotation from
fromDirection
to
toDirection
.
InverseReturns the Inverse of
rotation
.
LerpInterpolates between
a
and
b
by
t
and normalizes the result afterwards.
LerpUnclampedInterpolates between
a
and
b
by
t
and normalizes the result afterwards. The parameter
t
is not clamped.
LookRotationCreates a rotation with the specified
forward
and
upwards
directions.
NormalizeConverts this quaternion to a quaternion with the same orientation but with a magnitude of 1.0.
RotateTowardsRotates a rotation
from
towards
to
.
SlerpSpherically linear interpolates between unit quaternions
a
and
b
by a ratio of
t
.
SlerpUnclampedSpherically linear interpolates between unit quaternions
a
and
b
by t.

Operators

Operator

Description

operator ==Are two quaternions equal to each other?
QuaternionConverts a quaternion to a Quaternion.
quaternionConverts a Quaternion to a quaternion.
operator *Combines rotations
lhs
and
rhs
.