Vector2

Constructors

NameDescription
new(x: number,y: number)Returns a Vector2 from the given x and y components. Both parameters default to 0 if not provided.
zeroA ready-made Vector2 of (0, 0).
oneA ready-made Vector2 of (1, 1).
xAxisA ready-made unit Vector2 of (1, 0).
yAxisA ready-made unit Vector2 of (0, 1).

Code Snippet

local moveInput = Vector2.new(1, 1)
print(moveInput.Magnitude) --> 1.4142135623731

local direction = moveInput.Unit
print(direction) --> 0.70710678118655, 0.70710678118655

local a = Vector2.new(3, 4)
local b = Vector2.new(1, 0)
print(a:Dot(b)) --> 3
print(a:Lerp(b, 0.5)) --> 2, 2

Properties

NameTypeDescription
XnumberThe x-coordinate of the Vector2, corresponding to the first argument passed to Vector2.new(). Read-only.
YnumberThe y-coordinate of the Vector2, corresponding to the second argument passed to Vector2.new(). Read-only.
MagnitudenumberThe length of the Vector2, computed as math.sqrt(X^2 + Y^2). Read-only.
UnitVector2A normalized copy of the Vector2 - one with the same direction as the original but a magnitude of 1. Undefined (contains nan values) for a zero vector. Read-only.

Methods

NameParametersReturnsDescription
Cross(other: Vector2):numberother: Vector2numberReturns the 2D cross product of this vector with other, computed as self.X * other.Y - self.Y * other.X.
Abs():Vector2VoidVector2Returns a new vector made from the absolute value of each component.
Ceil():Vector2VoidVector2Returns a new vector with each component rounded up to the nearest integer.
Floor():Vector2VoidVector2Returns a new vector with each component rounded down to the nearest integer.
Sign():Vector2VoidVector2Returns a new vector with each component replaced by its sign: -1, 0, or 1.
Angle(other: Vector2,isSigned: boolean):numberother: Vector2, isSigned: boolean (optional, default false)numberReturns the angle in radians between this vector and other. When isSigned is true, the result may be negative to indicate rotational direction.
Dot(v: Vector2):numberv: Vector2numberReturns the scalar dot product of this vector and v, computed as self.X * v.X + self.Y * v.Y.
Lerp(v: Vector2,alpha: number):Vector2v: Vector2, alpha: numberVector2Returns a Vector2 linearly interpolated between this vector and v by the fraction alpha.
Max(...others: Vector2):Vector2others: one or more Vector2Vector2Returns a Vector2 with each component set to the highest among the respective components of this vector and the given vectors.
Min(...others: Vector2):Vector2others: one or more Vector2Vector2Returns a Vector2 with each component set to the lowest among the respective components of this vector and the given vectors.
FuzzyEq(other: Vector2,epsilon: number):booleanother: Vector2, epsilon: number (optional, default 1e-5)booleanReturns true if the X and Y components of other are each within epsilon of this vector's corresponding component.