Rotation

Summary

Represents a rotation or orientation in 3D space. See https://docs.unity3d.com/ScriptReference/Quaternion.html for further documentation

Class Properties

NameReturn TypeDescription

zero

Rotation Read-only

A rotation of zero in all axes

left

Rotation Read-only

A 90 degree anti-clockwise rotation in the y axis (yaw)

right

Rotation Read-only

A 90 degree clockwise rotation in the y axis (yaw)

up

Rotation Read-only

A 90 degree clockwise rotation in the x axis (pitch)

down

Rotation Read-only

A 90 degree anti-clockwise rotation in the x axis (pitch)

Instance Properties

NameReturn TypeDescription

this[index]

number Read/Write

The amount of rotation in degrees around a single axis (0=x, 1=y, 2=z)

x

number Read/Write

The amount of rotation around the x axis in degrees

y

number Read/Write

The amount of rotation around the y axis in degrees

z

number Read/Write

The amount of rotation around the z axis in degrees

normalized

Rotation Read-only

Converts this rotation to one with the same orientation but with a magnitude of 1

inverse

Rotation Read-only

Returns the Inverse of this rotation

angle

number Read/Write

The angle in degrees of the angle-axis representation of this rotation

axis

Vector3 Read/Write

The axis part of the angle-axis representation of this rotation

Class Methods

Rotation:New(x, y, z)

Creates a new Rotation

Returns: Rotation

Parameters:

NameTypeDefaultDescription

x

number

0

The angle of rotation on the x axis in degrees

y

number

0

The angle of rotation on the y axis in degrees

z

number

0

The angle of rotation on the z axis in degrees

Example

myRotation = Rotation:New(45, -90, 0)

Rotation:Angle(a, b)

Returns the angle in degrees between two rotations

Returns: number (Returns the angle in degrees between two rotations)

Parameters:

NameTypeDefaultDescription

a

The first rotation angle

b

The second rotation angle

Example

result = Rotation:Angle(a, b)

Rotation:AngleAxis(angle, axis)

Creates a rotation which rotates angle degrees around axis

Returns: Rotation (Returns a Quaternion that represents the rotation)

Parameters:

NameTypeDefaultDescription

angle

number

The angle in degrees

axis

The axis of rotation

Example

result = Rotation:AngleAxis(angle, axis)

Rotation:Dot(a, b)

The dot product between two rotations

Returns: number (Returns the dot product between two rotations)

Parameters:

NameTypeDefaultDescription

a

The first rotation

b

The second rotation

Example

result = Rotation:Dot(a, b)

Rotation:FromToRotation(from, to)

Creates a rotation which rotates from fromDirection to toDirection

Returns: Rotation (Returns a Quaternion that represents the rotation)

Parameters:

NameTypeDefaultDescription

from

The initial direction vector

to

The target direction vector

Example

result = Rotation:FromToRotation(from, to)

Rotation:Lerp(a, b, t)

Interpolates between a and b by t and normalizes the result afterwards. The parameter t is clamped to the range [0, 1]

Returns: Rotation (Interpolated rotation)

Parameters:

NameTypeDefaultDescription

a

The first rotation

b

The second rotation

t

number

A ratio between 0 and 1

Example

result = Rotation:Lerp(a, b, t)

Rotation:LerpUnclamped(a, b, t)

Interpolates between a and b by t and normalizes the result afterwards. The parameter t is not clamped

Returns: Rotation (Interpolated rotation)

Parameters:

NameTypeDefaultDescription

a

The first rotation

b

The second rotation

t

number

A ratio between 0 and 1

Example

result = Rotation:LerpUnclamped(a, b, t)

Rotation:LookRotation(forward)

Creates a rotation with the specified forward and upwards directions

Returns: Rotation (Rotation with specified forward direction)

Parameters:

NameTypeDefaultDescription

forward

Vector3 forward direction

Example

result = Rotation:LookRotation(forward)

Rotation:LookRotation(forward, up)

Creates a rotation with the specified forward and upwards directions

Returns: Rotation (Rotation with specified forward and up directions)

Parameters:

NameTypeDefaultDescription

forward

Vector3 forward direction

up

Vector3 up direction

Example

result = Rotation:LookRotation(forward, up)

Rotation:Normalize(a)

Converts this quaternion to one with the same orientation but with a magnitude of 1

Returns: Rotation (Normalized rotation)

Parameters:

NameTypeDefaultDescription

a

The input rotation

Example

result = Rotation:Normalize(a)

Rotation:RotateTowards(from, to, maxDegreesDelta)

Rotates a rotation from towards to

Returns: Rotation (Rotation rotated from towards to)

Parameters:

NameTypeDefaultDescription

from

Rotation from

to

Rotation to

maxDegreesDelta

number

Max degrees delta

Example

result = Rotation:RotateTowards(to, maxDegreesDelta)

Rotation:Slerp(a, b, t)

Spherically interpolates between quaternions a and b by ratio t. The parameter t is clamped to the range [0, 1]

Returns: Rotation (Spherically interpolated rotation)

Parameters:

NameTypeDefaultDescription

a

The first rotation

b

The second rotation

t

number

A ratio between 0 and 1

Example

result = Rotation:Slerp(a, b, t)

Rotation:SlerpUnclamped(a, b, t)

Spherically interpolates between a and b by t. The parameter t is not clamped

Returns: Rotation (Spherically interpolated rotation)

Parameters:

NameTypeDefaultDescription

a

The first rotation

b

The second rotation

t

number

A ratio

Example

result = Rotation:SlerpUnclamped(a, b, t)

Instance Methods

rotation:SetFromToRotation(fromDirection, toDirection)

Creates a rotation which rotates from one direction to another

Returns: Rotation (A rotation that would change one direction to the other)

Parameters:

NameTypeDefaultDescription

fromDirection

The starting direction

toDirection

The target direction

Example

newRot = myRotation:SetFromToRotation(Vector3:New(0, 5, 5), Vector3:New(5, 5, 0))

rotation:SetLookRotation(view)

Creates a rotation with the specified forward directions

Returns: Rotation (The new Rotation)

Parameters:

NameTypeDefaultDescription

view

The direction to look in

Example

result = myRotation:SetLookRotation(view)

rotation:SetLookRotation(view, up)

Creates a rotation with the specified forward and upwards directions

Returns: Rotation (The new Rotation)

Parameters:

NameTypeDefaultDescription

view

The direction to look in

up

The vector that defines in which direction is up

Example

result = myRotation:SetLookRotation(view, up)

rotation:Multiply(other)

Combines two rotations

Returns: Rotation (The rotation that represents applying both rotations in turn)

Parameters:

NameTypeDefaultDescription

other

The other rotation

Example

result = myRotation:Multiply(anotherRotation)

rotation:NotEquals(other)

Determines whether this rotation is not equal to the specified rotation

Returns: boolean (true if this rotation is not equal to the specified rotation; otherwise, false)

Parameters:

NameTypeDefaultDescription

other

The rotation to compare

Example

if myRotation:NotEquals(rot2) then print("rotations are different") end

Last updated