Vector3

Summary

A position or offset in 3D space. See https://docs.unity3d.com/ScriptReference/Vector3.html for more detail on many of these methods or properties

Class Properties

NameReturn TypeDescription

back

Vector3 Read-only

A vector of -1 in the z axis

down

Vector3 Read-only

A vector of -1 in the y axis

forward

Vector3 Read-only

A vector of 1 in the z axis

left

Vector3 Read-only

A vector of -1 in the x axis

negativeInfinity

Vector3 Read-only

A vector of -infinity in all axes

one

Vector3 Read-only

A vector of 1 in all axes

positiveInfinity

Vector3 Read-only

A vector of infinity in all axes

right

Vector3 Read-only

A vector of 1 in the x axis

up

Vector3 Read-only

A vector of 1 in the y axis

zero

Vector3 Read-only

A vector of 0 in all axes

Instance Properties

NameReturn TypeDescription

this[index]

number Read/Write

The component at the specified index

x

number Read/Write

The x coordinate

y

number Read/Write

The y coordinate

z

number Read/Write

The z coordinate

magnitude

number Read/Write

Returns the length of this vector

sqrMagnitude

number Read/Write

Returns the squared length of this vector

normalized

Vector3 Read-only

Returns a vector with the same direction but with a length of 1

Class Methods

Vector3:New(x, y, z)

Creates a new vector based on x, y and z position

Returns: Vector3

Parameters:

NameTypeDefaultDescription

x

number

The x coordinate

y

number

The y coordinate

z

number

The z coordinate

Example

newVector = Vector3:New(1, 2, 3)

Vector3:Cross(a, b)

Returns the cross product of two vectors

Returns: Vector3

Parameters:

NameTypeDefaultDescription

a

The first vector

b

The second vector

Example

crossProduct = Vector3:Cross(firstVector, secondVector)

Vector3:Lerp(a, b, t)

Linearly interpolates between two points

Returns: Vector3 (A point somewhere between a and b based on the value of t)

Parameters:

NameTypeDefaultDescription

a

The first point

b

The second point

t

number

The value between 0 and 1 that controls how far between a and b the new point is

Example

newPoint = Vector3:Lerp(pointA, PointB, 0.25)

Vector3:LerpUnclamped(a, b, t)

Linearly interpolates (or extrapolates) between two points

Returns: Vector3 (A point somewhere between a and b based on the value of t)

Parameters:

NameTypeDefaultDescription

a

The first point

b

The second point

t

number

The value that controls how far between (or beyond) a and b the new point is

Example

newPoint = Vector3:Lerp(pointA, PointB, 0.25)

Vector3:Max(a, b)

Creates a vector made from the largest components of the inputs

Returns: Vector3

Parameters:

NameTypeDefaultDescription

a

The first vector

b

The second vector

Example

result = Vector3:Max(firstVector, secondVector

Vector3:Min(a, b)

Creates a vector made from the smallest components of the inputs

Returns: Vector3

Parameters:

NameTypeDefaultDescription

a

The first vector

b

The second vector

Example

result = Vector3:Min(firstVector, secondVector

Vector3:Slerp(a, b, t)

Spherically interpolates between two vectors

Returns: Vector3 (A point somewhere between a and b based on the value of t)

Parameters:

NameTypeDefaultDescription

a

The first point

b

The second point

t

number

The value that controls how far between (or beyond) a and b the new point is

Example

newPoint = Vector3:Lerp(pointA, PointB, 0.25)

Vector3:SlerpUnclamped(a, b, t)

Spherically interpolates (or extrapolates) between two vectors

Returns: Vector3 (A point somewhere between a and b based on the value of t)

Parameters:

NameTypeDefaultDescription

a

The first point

b

The second point

t

number

The value that controls how far between (or beyond) a and b the new point is

Example

newPoint = Vector3:Lerp(pointA, PointB, 0.25)

Instance Methods

vector3:Angle(other)

The unsigned angle in degrees between this vector and another

Returns: number

Parameters:

NameTypeDefaultDescription

other

The other vector

Example

angle = myVector:Angle(otherVector)

vector3:ClampMagnitude(maxLength)

Returns a vector with the same direction but with it's length clamped to a maximum

Returns: Vector3

Parameters:

NameTypeDefaultDescription

maxLength

number

The maximum length of the returned vector

Example

clampedVector = myVector:ClampMagnitude(5)

vector3:Distance(other)

Returns the distance between two points

Returns: number

Parameters:

NameTypeDefaultDescription

other

The other vector

Example

distance = Vector3:Distance(firstPoint, secondPoint)

vector3:MoveTowards(target, maxDistanceDelta)

Moves a point towards a target point

Returns: Vector3

Parameters:

NameTypeDefaultDescription

target

The target point

maxDistanceDelta

number

The maximum distance to move towards the target point

Example

position = position:MoveTowards(PointB, 0.25)

vector3:Project(other)

Projects this vector onto another

Returns: Vector3

Parameters:

NameTypeDefaultDescription

other

The other vector

Example

newVector = myVector:Project(otherVector)

vector3:ProjectOnPlane(planeNormal)

Projects this vector onto a plane defined by a normal orthogonal to the plane

Returns: Vector3

Parameters:

NameTypeDefaultDescription

planeNormal

The normal vector of the plane

Example

newVector = myVector:ProjectOnPlane(planeNormal)

vector3:Reflect(normal)

Reflects a vector off the vector defined by a normal

Returns: Vector3

Parameters:

NameTypeDefaultDescription

normal

The normal vector

Example

newVector = myVector:Reflect(normalVector)

vector3:RotateTowards(target, maxRadiansDelta, maxMagnitudeDelta)

Moves this vector towards another with a maximum change in angle

Returns: Vector3

Parameters:

NameTypeDefaultDescription

target

The target vector

maxRadiansDelta

number

The maximum change in angle

maxMagnitudeDelta

number

The maximum allowed change in vector magnitude for this rotation

Example

newVector = myVector:RotateTowards(targetVector, Math.pi / 10, 0.25)

vector3:ScaleBy(other)

Multiplies two vectors component-wise

Returns: Vector3

Parameters:

NameTypeDefaultDescription

other

The other vector

Example

result = myVector:Scale(secondVector

vector3:SignedAngle(other, axis)

Returns the signed angle in degrees between two points and the origin

Returns: number

Parameters:

NameTypeDefaultDescription

other

The other vector

axis

The axis around which the vectors are rotated

Example

angle = myVector:SignedAngle(otherVector, axis)

vector3:Add(other)

Adds two vectors

Returns: Vector3

Parameters:

NameTypeDefaultDescription

other

The other vector

Example

result = myVector:Add(secondVector)

vector3:Add(x, y, z)

Adds x, y and z values to this vector

Returns: Vector3

Parameters:

NameTypeDefaultDescription

x

number

The x value

y

number

The y value

z

number

The z value

Example

result = myVector:Add(1, 2, 3)

vector3:Subtract(other)

Subtracts a Vector3 from this vector

Returns: Vector3

Parameters:

NameTypeDefaultDescription

other

The vector to subtract

Example

result = myVector:Subtract(otherVector)

vector3:Subtract(x, y, z)

Subtracts x, y and z values from this vector

Returns: Vector3

Parameters:

NameTypeDefaultDescription

x

number

The x value

y

number

The y value

z

number

The z value

Example

result = myVector:Subtract(1, 2, 3)

vector3:Multiply(value)

Multiplies this vector by a scalar value

Returns: Vector3

Parameters:

NameTypeDefaultDescription

value

number

The scalar value

Example

result = myVector:Multiply(2)

vector3:ScaleBy(x, y, z)

Multiplies this vector by x, y and z values component-wise

Returns: Vector3

Parameters:

NameTypeDefaultDescription

x

number

The x value

y

number

The y value

z

number

The z value

Example

result = myVector:Multiply(2, 3, 4)

vector3:Divide(value)

Divides this vector by a scalar value

Returns: Vector3

Parameters:

NameTypeDefaultDescription

value

number

The scalar value

Example

result = myVector:Divide(2)

vector3:NotEquals(other)

Is this vector not equal to another?

Returns: boolean

Parameters:

NameTypeDefaultDescription

other

The other vector

Example

if myVector:NotEquals(Vector3.zero) then print("Vector is not zero") end

vector3:NotEquals(x, y, z)

Is this vector not equal to these x, y and z values?

Returns: boolean

Parameters:

NameTypeDefaultDescription

x

number

The x value

y

number

The y value

z

number

The z value

Example

if myVector:NotEquals(1, 2, 3) then print("Vector is not 1,2,3") end

Last updated