Vector2
Represents a 2-component vector.
Behavior
Vector2 stores X, Y components for positions, directions, and dimensions.
Components are read-only. Arithmetic returns new vectors; addition is
componentwise and multiplication by a number scales the vector.
Summary
The vector length.
Returns a vector with the same direction and length 1.
The X component.
The Y component.
Constructors
new
Vector2.new(x: number?, y: number?): Vector2Creates a vector from X, Y components. Omitted components default to 0.
one
Vector2.one: Vector2The vector (1, 1).
zero
Vector2.zero: Vector2The vector (0, 0).
Properties
Magnitude
Vector2.Magnitude: numberThe vector length. The magnitude of the difference between two positions is their distance.
Unit
Vector2.Unit: Vector2Returns a vector with the same direction and length 1. Check for zero magnitude before normalizing.
X
Vector2.X: numberThe X component. Read-only.
Y
Vector2.Y: numberThe Y component. Read-only.
Methods
Dot
Vector2:Dot(other: Vector2): numberReturns the dot product. For nonzero vectors, a positive result indicates an angle below 90 degrees, 0 indicates perpendicular directions, and a negative result indicates an angle above 90 degrees.
Lerp
Vector2:Lerp(other: Vector2, alpha: number): Vector2Interpolates toward another vector. A fraction of 0 returns this vector; 1 returns the target vector.
Operators
__add · __div · __eq · __mul · __sub · __tostring · __type · __unm
Example
local vector = Vector2.new(1, 2)
print(vector.Magnitude)Used by
BillboardGui · Camera · Frame · Gradient · GuiService · ImageButton · ImageLabel · InputService · LinearVelocity · ProximityPrompt · SceneFrame · ScrollingFrame · Shadow · Stack · SurfaceGui · TextBox · TextButton · TextLabel · TextService · UIGridLayout · UIListLayout · UIPageLayout · UISizeConstraint · VRService
Related APIs
Vector3 · UDim2 · InputObject