Hand-written by Joseph San Nicolas, Roblox Developer
Vector2
Constructors
| Name | Description |
| new(x: number,y: number) | Returns a Vector2 from the given x and y components. Both parameters default to 0 if not provided. |
| zero | A ready-made Vector2 of (0, 0). |
| one | A ready-made Vector2 of (1, 1). |
| xAxis | A ready-made unit Vector2 of (1, 0). |
| yAxis | A 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
| Name | Type | Description |
| X | number | The x-coordinate of the Vector2, corresponding to the first argument passed to Vector2.new(). Read-only. |
| Y | number | The y-coordinate of the Vector2, corresponding to the second argument passed to Vector2.new(). Read-only. |
| Magnitude | number | The length of the Vector2, computed as math.sqrt(X^2 + Y^2). Read-only. |
| Unit | Vector2 | A 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
| Name | Parameters | Returns | Description |
| Cross(other: Vector2):number | other: Vector2 | number | Returns the 2D cross product of this vector with other, computed as self.X * other.Y - self.Y * other.X. |
| Abs():Vector2 | Void | Vector2 | Returns a new vector made from the absolute value of each component. |
| Ceil():Vector2 | Void | Vector2 | Returns a new vector with each component rounded up to the nearest integer. |
| Floor():Vector2 | Void | Vector2 | Returns a new vector with each component rounded down to the nearest integer. |
| Sign():Vector2 | Void | Vector2 | Returns a new vector with each component replaced by its sign: -1, 0, or 1. |
| Angle(other: Vector2,isSigned: boolean):number | other: Vector2, isSigned: boolean (optional, default false) | number | Returns 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):number | v: Vector2 | number | Returns the scalar dot product of this vector and v, computed as self.X * v.X + self.Y * v.Y. |
| Lerp(v: Vector2,alpha: number):Vector2 | v: Vector2, alpha: number | Vector2 | Returns a Vector2 linearly interpolated between this vector and v by the fraction alpha. |
| Max(...others: Vector2):Vector2 | others: one or more Vector2 | Vector2 | Returns a Vector2 with each component set to the highest among the respective components of this vector and the given vectors. |
| Min(...others: Vector2):Vector2 | others: one or more Vector2 | Vector2 | Returns 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):boolean | other: Vector2, epsilon: number (optional, default 1e-5) | boolean | Returns true if the X and Y components of other are each within epsilon of this vector's corresponding component. |