Data type
Vector3
Represents a 3D value with a direction and magnitude.
About Vector3
The Vector3 data type represents a vector in 3D space, typically
used as a point in 3D space or the dimensions of a rectangular prism.
Vector3 supports basic component-based arithmetic operations (sum,
difference, product, and quotient) and these operations can be applied on the
left or right hand side to either another Vector3 or a number. It
also features methods for common vector operations, such as
Cross() and Dot().
Alternatively to Vector3, consider using the methods and properties
of the vector library.
Some example usages of Vector3 are the
Position, Rotation, and
Size of parts, for example:
local part = Instance.new("Part")
part.Position = part.Position + Vector3.new(5, 2, 10) -- Move part by (5, 2, 10)
print(part.Position) --> 5, 2, 10
Vector3 is also commonly used when constructing more complex 3D
data types such as CFrame. Many of these data types' methods will
use a Vector3 within their parameters, such as
CFrame.PointToObjectSpace.
Constructors 3
Properties 10
Unit
: Vector3A normalized copy of theVector3- one that has the same direction as the original but a magnitude of1.A normalized copy of the
Vector3— one that has the same direction as the original but a magnitude of1. This is useful when you need only the direction of a vector without its length, for example to get a movement direction regardless of speed.If the vector has a magnitude of
0(i.e. all components are zero), the resultingUnitvector will haveNaNcomponents. CheckMagnitudebefore usingUnitwhen the vector may be zero-length.local v = Vector3.new(3, 4, 0) print(v.Unit) --> 0.6, 0.8, 0 print(v.Unit.Magnitude) --> 1
Methods 11
Abs
() → Vector3Returns a new vector from the absolute values of the original's components.Returns a new vector from the absolute values of the original's components. For example, a vector of
(-2, 4, -6)returns a vector of(2, 4, 6).Ceil
() → Vector3Returns a new vector from the ceiling of the original's components.Returns a new vector from the ceiling of the original's components. For example, a vector of
(-2.6, 5.1, 8.8)returns a vector of(-2, 6, 9).Floor
() → Vector3Returns a new vector from the floor of the original's components.Returns a new vector from the floor of the original's components. For example, a vector of
(-2.6, 5.1, 8.8)returns a vector of(-3, 5, 8).Sign
() → Vector3Returns a new vector from the sign (-1, 0, or 1) of the original's components.Returns a new vector from the sign (-1, 0, or 1) of the original's components. For example, a vector of
(-2.6, 5.1, 0)returns a vector of(-1, 1, 0).Cross
(other: Vector3) → Vector3Returns the cross product of the two vectors.Returns the cross product of this vector and
other. The resulting vector is perpendicular to both input vectors and has a magnitude equal to the area of the parallelogram they span. The direction follows the right-hand rule: if you curl the fingers of your right hand fromselftowardother, your thumb points in the direction of the result.The cross product is commonly used to find normals to surfaces, determine the axis of rotation between two directions, and test whether two vectors are parallel (cross product of parallel vectors is the zero vector).
local a = Vector3.new(1, 0, 0) local b = Vector3.new(0, 1, 0) print(a:Cross(b)) --> 0, 0, 1 print(b:Cross(a)) --> 0, 0, -1Angle
(other: Vector3, axis: Vector3 = nil) → numberReturns the angle in radians between the two vectors. If you provide an axis, it determines the sign of the angle.Returns the angle in radians between this vector and
other. The input vectors do not need to be unit vectors. Without anaxisargument, the returned angle is always in the range[0, math.pi](unsigned).If you provide an
axisVector3, the returned angle is signed: positive when the rotation fromselftootherfollows the right-hand rule aroundaxis, and negative otherwise. The signed result is in the range[-math.pi, math.pi]. Theaxisis used only to determine the sign; the plane of rotation is still defined byselfandother.local a = Vector3.new(1, 0, 0) local b = Vector3.new(0, 1, 0) print(a:Angle(b)) --> 1.5707963... (pi/2) print(a:Angle(b, Vector3.new(0, 0, 1))) --> 1.5707963... (positive, CCW about +Z) print(b:Angle(a, Vector3.new(0, 0, 1))) --> -1.5707963... (negative, CW about +Z)Dot
(other: Vector3) → numberReturns a scalar dot product of the two vectors.Returns the scalar dot product of this vector and
other, computed asself.X * other.X + self.Y * other.Y + self.Z * other.Z.The dot product is useful for determining the relationship between two directions: it equals the product of their magnitudes multiplied by the cosine of the angle between them. For unit vectors, a result of
1means they point in the same direction,0means they are perpendicular, and-1means they point in opposite directions.local a = Vector3.new(1, 0, 0) local b = Vector3.new(0, 1, 0) print(a:Dot(b)) --> 0 local c = Vector3.new(1, 2, 3) local d = Vector3.new(4, 5, 6) print(c:Dot(d)) --> 32FuzzyEq
(other: Vector3, epsilon: number = 0.00001 aka 1e-5) → boolReturnstrueif the difference between the squared magnitude of the two vectors is withinepsilon.epsilonis scaled relative to the magnitude, rather than an absolute epsilon.Returns
trueif the two vectors are approximately equal within the tolerance defined byepsilon. The comparison is performed per-component using a hybrid epsilon that scales relative to the magnitude of each component, making it suitable for both small and large values. The defaultepsilonof0.00001(1e-5) works well for most cases.local a = Vector3.new(1, 2, 3) local b = Vector3.new(1.000001, 2, 3) print(a:FuzzyEq(b)) --> true (within default epsilon) print(a:FuzzyEq(b, 1e-8)) --> false (tighter tolerance)
Math operations
- Vector3 + Vector3 → Vector3
- Vector3 - Vector3 → Vector3
- Vector3 * Vector3 → Vector3
- Vector3 / Vector3 → Vector3
- Vector3 // Vector3 → Vector3
- Vector3 * number → Vector3
- Vector3 / number → Vector3
- Vector3 // number → Vector3
Reference text from the Roblox Creator Documentation, licensed CC BY 4.0, reorganized by rbxtoolshub.com · Studio 0.743.0.7431133.