Data type
Vector2
Represents a 2D value with direction and magnitude.
About Vector2
Constructors 1
Vector2.new
(x: number, y: number)Returns aVector2from the given x and y components.
Properties 4
X
: numberThe x-coordinate of theVector2.The x-coordinate of the
Vector2, corresponding to the first argument passed toVector2.new. This property is read-only.local v = Vector2.new(3, 4) print(v.X) --> 3Y
: numberThe y-coordinate of theVector2.The y-coordinate of the
Vector2, corresponding to the second argument passed toVector2.new. This property is read-only.local v = Vector2.new(3, 4) print(v.Y) --> 4Unit
: Vector2A normalized copy of theVector2.A normalized copy of the
Vector2— one that has the same direction as the original but a magnitude of 1. If the vector has zero magnitude the result is undefined (containsnanvalues). This property is read-only.local v = Vector2.new(3, 4) print(v.Unit) --> 0.6, 0.8 print(v.Unit.Magnitude) --> 1
Methods 11
Cross
(other: Vector2) → numberReturns the cross product of the two vectors.Returns the 2D cross product of this vector with
other, computed asself.X * other.Y - self.Y * other.X. UnlikeVector3.Crosswhich returns a vector, the 2D cross product returns a scalar representing the signed magnitude of the perpendicular (Z) component. The result is positive whenotheris counter-clockwise fromself, negative when clockwise, and zero when the vectors are parallel.local a = Vector2.new(1, 0) local b = Vector2.new(0, 1) print(a:Cross(b)) --> 1 print(b:Cross(a)) --> -1Abs
() → Vector2Returns 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)returns a vector of(2, 4).Ceil
() → Vector2Returns 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)returns a vector of(-2, 6).Floor
() → Vector2Returns 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)returns a vector of(-3, 5).Sign
() → Vector2Returns 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)returns a vector of(-1, 1).Angle
(other: Vector2, isSigned: boolean = false) → numberReturns the angle in radians between the two vectors.Returns the angle in radians between the two vectors. Specify
truefor the optionalisSignedboolean if you want a signed angle. By default, the method returns the absolute value. Signed angles are a negative when going clockwise. Values are in the range[0, pi]for absolute angles and[-pi, pi]for signed angles.Dot
(v: Vector2) → numberReturns a scalar dot product of the two vectors.Returns the scalar dot product of this vector and
v, computed asself.X * v.X + self.Y * v.Y. The result equals the product of the two magnitudes multiplied by the cosine of the angle between them. A positive value means the vectors point in roughly the same direction; zero means they are perpendicular; negative means they point in roughly opposite directions.local a = Vector2.new(1, 0) local b = Vector2.new(0, 1) print(a:Dot(b)) --> 0 (perpendicular) print(a:Dot(a)) --> 1 (parallel, equal to Magnitude^2)Lerp
(v: Vector2, alpha: number) → Vector2Returns aVector2linearly interpolated between thisVector2and the given goal by the given alpha.Returns a
Vector2linearly interpolated between thisVector2and the given goalvby the fractionalpha, using the formulaself + (v - self) * alpha. Analphaof 0 returnsself, analphaof 1 returnsv, and values between 0 and 1 return a point in between. Note thatalphais not clamped to[0, 1]— values outside that range extrapolate beyond the two endpoints.local a = Vector2.new(0, 0) local b = Vector2.new(4, 8) print(a:Lerp(b, 0.5)) --> 2, 4FuzzyEq
(other: Vector2, epsilon: number = 0.00001 (1e-5)) → boolReturnstrueif the X and Y components of the otherVector2are within epsilon units of each corresponding component of thisVector2.Returns
trueif the X and Y components of the otherVector2are withinepsilonunits of each corresponding component of thisVector2. The comparison is per-component: bothmath.abs(self.X - other.X)andmath.abs(self.Y - other.Y)must be less than or equal toepsilon. The optionalepsilonparameter defaults to1e-5(0.00001) and must be a positive value no greater than0.1.local a = Vector2.new(1, 2) local b = Vector2.new(1.000009, 2.000009) print(a:FuzzyEq(b)) --> true (within default 1e-5) print(a:FuzzyEq(b, 0.000001)) --> false (tighter epsilon)
Math operations
- Vector2 + Vector2 → Vector2
- Vector2 - Vector2 → Vector2
- Vector2 * Vector2 → Vector2
- Vector2 / Vector2 → Vector2
- Vector2 * number → Vector2
- Vector2 / number → Vector2
Reference text from the Roblox Creator Documentation, licensed CC BY 4.0, reorganized by rbxtoolshub.com · Studio 0.743.0.7431133.