Skip to main content

Vector3

Defines a 3D vector, that can be used to represent a position, direction or rotation of an object in three-dimensions (x,y,z).

Constructors

Vector3.new

Creates a new Vector3 with the given x, y and z values.

Example

-- Creates a new Vector3 with x=1, y=2, z=3, and assigns the vector to variable "a"
local a = Vector3.new(1, 2, 3)
local b = Vector3.new()

print(a) -- Prints '(1, 2, 3)'
print(b) -- Prints '(0, 0, 0)'

Properties

x

The x component of the Vector3.

Example

local a = Vector3.new(1, 2, 3)

print(a.x) -- Prints '1'

y

The y component of the Vector3.

Example

local a = Vector3.new(1, 2, 3)

print(a.y) -- Prints '2'

z

The z component of the Vector3.

Example

local a = Vector3.new(1, 2, 3)

print(a.z) -- Prints '3'

Functions

angle(a, b)

Returns the angle (in degrees) between the two vectors.

Example

local a = Vector3.new(1, 2, 3)
local b = Vector3.new(4, 5, 6)

print(Vector3.angle(a, b)) -- Prints '12.941'

dot(a, b)

Returns the dot product of the two vectors.

Example

local a = Vector3.new(1, 2, 3)
local b = Vector3.new(4, 5, 6)

print(Vector3.dot(a, b)) -- Prints '32'

cross(a, b)

Returns the cross product of the two vectors.

Example

local a = Vector3.new(1, 2, 3)
local b = Vector3.new(4, 5, 6)

print(Vector3.cross(a, b)) -- Prints '(-3, 6, -3)'

distance(a, b)

Returns the distance between the two vectors.

Example

local a = Vector3.new(1, 2, 3)
local b = Vector3.new(4, 5, 6)

print(Vector3.distance(a, b)) -- Prints '5.196'

lerp(from, to, t)

Returns a Vector3 where the value of each component is linearly interpolated from the input vectors, by the value specified in "t" (which should be between 0 and 1).

Example

local a = Vector3.new(1, 1, 1)
local b = Vector3.new(2, 2, 2)

print(Vector3.lerp(a, b, 0.5)) -- Prints '(1.5, 1.5, 1.5)'

magnitude(a)

Returns the length of the input vector.

Example

local a = Vector3.new(1, 1, 1)

print(Vector3.magnitude(a)) -- Prints '1.732'

max(a, b)

Returns a Vector3 where the value of each component is the maximum value from the respective component in each of the input vectors.

Example

local a = Vector3.new(1, 2, 3)
local b = Vector3.new(3, 2, 1)

print(Vector3.max(a, b)) -- Prints '(3, 2, 3)'

min(a, b)

Returns a Vector3 where the value of each component is the minimum value from the respective component in each of the input vectors.

Example

local a = Vector3.new(1, 2, 3)
local b = Vector3.new(3, 2, 1)

print(Vector3.min(a, b)) -- Prints '(1, 2, 1)'

normalize(a)

Returns a Vector3 pointing in the same direction as the input vector, but with a length of 1.

Example

local a = Vector3.new(3, 4, 5)

print(Vector3.normalize(a)) -- Prints '(0.424, 0.566, 0.707)'

sqrMagnitude(a)

Returns the squared length of the input vector.

Example

local a = Vector3.new(1, 2, 3)

print(Vector3.sqrMagnitude(a)) -- Prints '14'

Math Operations

Vector3 + Vector3

Adds each of the components of the vector.

Example

local a = Vector3.new(1, 2, 3)
local b = Vector3.new(3, 4, 5)

print(a + b) -- Prints '(4, 6, 8)'

Vector3 - Vector3

Subtracts each of the components of the vector.

Example

local a = Vector3.new(1, 2, 3)
local b = Vector3.new(3, 4, 5)

print(a - b) -- Prints '(-2, -2, -2)'

Vector3 * number

Multiplies each of the components of the vector by the number.

Example

local a = Vector3.new(1, 2, 3)

print(a * 3) -- Prints '(3, 6, 9)'

Vector3 * Vector3

Multiplies each of the components of the first vector by the respective component on the second vector.

Example

local a = Vector3.new(1, 2, 3)
local b = Vector3.new(3, 4, 5)

print(a * b) -- Prints '(3, 8, 15)'

Vector3 / number

Divides each of the components of the vector by the number.

Example

local a = Vector3.new(3, 12, 15)

print(a / 3) -- Prints '(1, 4, 5)'

Vector3 / Vector3

Divides each of the components of the first vector by the respective component on the second vector.

Example

local a = Vector3.new(3, 12, 15)
local b = Vector3.new(3, 4, 5)

print(a / b) -- Prints '(1, 3, 3)'

Vector3 ^ Vector3

Returns the cross product of the two vectors.

Example

local a = Vector3.new(1, 2, 3)
local b = Vector3.new(4, 5, 6)

print(a ^ b) -- Prints '(-3, 6, -3)'

-Vector3

Returns the negative of the vector.

Example

local a = Vector4.new(1, -2, 3)

print(-a) -- Prints '(-1, 2, -3)'

#Vector3

Returns the length of the vector.

Example

local a = Vector3.new(1, 2, 3)

print(#a) -- Prints '3.742'

Constants

Vector3.zero

Returns a Vector3 where the value of each component is 0.

Example

print(Vector3.zero)    -- Prints '(0, 0, 0)'

Vector3.one

Returns a Vector3 where the value of each component is 1.

Example

print(Vector3.one)    -- Prints '(1, 1, 1)'

Vector3.up

Returns a Vector3 where the value of the y component is 1.

Example

print(Vector3.up)    -- Prints '(0, 1, 0)'

Vector3.down

Returns a Vector3 where the value of the y component is -1.

Example

print(Vector3.down)    -- Prints '(0, -1, 0)'

Vector3.left

Returns a Vector3 where the value of the x component is -1.

Example

print(Vector3.left)    -- Prints '(-1, 0, 0)'

Vector3.right

Returns a Vector3 where the value of the x component is 1.

Example

print(Vector3.right)    -- Prints '(1, 0, 0)'

Vector3.forward

Returns a Vector3 where the value of the z component is 1.

Example

print(Vector3.forward)    -- Prints '(0, 0, 1)'

Vector3.back

Returns a Vector3 where the value of the z component is -1.

Example

print(Vector3.back)    -- Prints '(0, 0, -1)'