PcoWSkbVqDnWTu_dm2ix
The Developer Hub is now deprecated and information on this page may no longer be accurate. To see our new and improved documentation, please click here. You can read more about the future of documentation here.
Collapse Sidebar

Vector3

Vector3

Vector3 describes a vector in 3D space, typically usually used as a point in 3D space or the dimensions of a rectangular prism. Vector3 supports basic component-based arithmetic operators: sum, difference, product, and quotient. These operations can be applied on the left or right hand side to either another Vector3 or a number. It also features functions for commonly used vector operations, such as cross and dot products.

Some example usages of Vector3 are the BasePart/Position|Position, BasePart/Rotation|Rotation and BasePart/Size|Size of BasePart|parts. Learning to set these properties are among the first things many developers will learn:

local part = workspace.Part
part.Position = part.Position + Vector3.new(5, 20, 100) -- Moves a part by this much

Vector3 is commonly used when constructing other more complex 3D data types, namely datatype/CFrame and datatype/Ray. Many of these data types’ functions will use a Vector3 within their parameters, such as CFrame:PointToObjectSpace or Ray:ClosestPoint. CFrame arithmetic also supports Vector3 for addition/subtraction.

See Also

  • datatype/Vector3int16, a lower precision implementation of 3D vectors used with Region3int16

Constructors

Vector3.new ( number x = 0, number y = 0, number z = 0 )

Constructs a new Vector3 using the given x, y, and z components.

Vector3.FromNormalId ( Enum normal )

Constructs a new Vector3 in a particular direction.

Vector3.FromAxis ( Enum axis )

Constructs a new Vector3 for a particular axis.

Properties

Vector3 Vector3.zero

A Vector3 with a magnitude of zero.

This API member is a constant, and must be accessed through the Vector3 global as opposed to an individual Vector3 object.

print(Vector3.zero) --> 0, 0, 0
Vector3 Vector3.one

A Vector3 with a value of 1 on every axis.

This API member is a constant, and must be accessed through the Vector3 global as opposed to an individual Vector3 object.

print(Vector3.one) --> 1, 1, 1
Vector3 Vector3.xAxis

A Vector3 with a value of 1 on the X axis.

This API member is a constant, and must be accessed through the Vector3 global as opposed to an individual Vector3 object.

print(Vector3.xAxis) --> 1, 0, 0
Vector3 Vector3.yAxis

A Vector3 with a value of 1 on the Y axis.

This API member is a constant, and must be accessed through the Vector3 global as opposed to an individual Vector3 object.

print(Vector3.yAxis) --> 0, 1, 0
Vector3 Vector3.zAxis

A Vector3 with a value of 1 on the Z axis.

This API member is a constant, and must be accessed through the Vector3 global as opposed to an individual Vector3 object.

print(Vector3.zAxis) --> 0, 0, 1
number Vector3.X

The x-coordinate of the Vector3.

number Vector3.Y

The y-coordinate of the Vector3.

number Vector3.Z

The z-coordinate of the Vector3.

number Vector3.Magnitude

The length of the Vector3.

Vector3 Vector3.Unit

A normalized copy of the vector - one which has the same direction as the original but a magnitude of 1.

Functions

Vector3 Vector3:Cross ( Vector3 other )

Returns the cross product of the two vectors.

number Vector3:Dot ( Vector3 other )

Returns a scalar dot product of the two vectors.

bool Vector3:FuzzyEq ( Vector3 other, number epsilon = 0.00001 aka 1e-5 )

Returns true if the other Vector3 falls within the epsilon radius of this Vector3.

Vector3 Vector3:Lerp ( Vector3 goal, number alpha )

Returns a Vector3 linearly interpolated between this Vector3 and the given goal Vector3 by the fraction alpha.

Note: the alpha value is not limited to the range [0, 1].

Vector3 Vector3:Max ( Tuple others... )

Returns a Vector3 where each component is the highest among the respective components of the provided Vector3s.

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

print(a:Max(b)) -- Vector3.new(2, 2, 2)
Vector3 Vector3:Min ( Tuple others... )

Returns a Vector3 where each component is the lowest among the respective components of the provided Vector3s.

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

print(a:Min(b)) -- Vector3.new(1, 1, 1)

Math Operations

Vector3 Vector3 + Vector3

Returns a new Vector3 with each component of the second added to the corresponding component of the first.

Vector3 Vector3 - Vector3

Returns a new Vector3 with each component of the second subtracted from the corresponding component of the first.

Vector3 Vector3 * Vector3

Returns a new Vector3 with each component of the second multiplied the corresponding component of the first.

Vector3 Vector3 / Vector3

Returns a new Vector3 with each component of the first divided by the corresponding component of the second.

Vector3 Vector3 * number

Returns the Vector3 with each component multiplied by the number.

Vector3 Vector3 / number

Returns the Vector3 with each component divided by the number.