Vector2
Vector2 stores two coordinates: x and y. It is suitable for points, sizes, directions, and screen coordinates.
Purpose and processing
Section titled “Purpose and processing”Vector2 is a generic two-number value. It gains meaning only in the receiving API: AgentInput interprets it as movement or look angles, UI uses it as a two-dimensional position, and math methods treat it as a regular vector.
Dot, Angle, Distance, Normalize, and arithmetic only perform calculations and do not change game state. Pass the result to the appropriate setter or method for it to take effect.
Quick example
Section titled “Quick example”local direction = Vector2.new(3, 4)local normalized = direction:Normalize()
print(normalized) -- (0.60, 0.80)print(direction) -- (3.00, 4.00)print(direction.magnitude) -- 5Normalize() returns a new vector and does not modify the source.
Creating a vector
Section titled “Creating a vector”Vector2.new(x: number, y: number): Vector2local position = Vector2.new(120, 80)Properties
Section titled “Properties”| Property | Type | Access | Description |
|---|---|---|---|
x |
number |
get/set |
X component. |
y |
number |
get/set |
Y component. |
magnitude |
number |
get |
Vector length. |
sqrMagnitude |
number |
get |
Squared vector length. |
normalized |
Vector2 |
get |
Normalized copy. |
magnitude, sqrMagnitude, and normalized are read-only. The x and y components are writable.
Ready-made values
Section titled “Ready-made values”| Value | Result |
|---|---|
Vector2.zero |
(0, 0) |
Vector2.one |
(1, 1) |
Operators
Section titled “Operators”local sum = Vector2.new(1, 2) + Vector2.new(3, 4) -- (4, 6)local difference = sum - Vector2.new(1, 2) -- (3, 4)Addition and subtraction between two Vector2 values are supported.
Methods
Section titled “Methods”Magnitude
Section titled “Magnitude”vector:Magnitude(): numberReturns the vector length. The result matches vector.magnitude.
Normalize
Section titled “Normalize”vector:Normalize(): Vector2Returns a new normalized copy without modifying vector. The vector.normalized getter provides the same kind of value.
vector:Dot(other: Vector2): numberReturns the dot product of two vectors.
vector:Angle(to: Vector2): numberReturns the angle between two directions in degrees. The result between (1, 0) and (0, 1) is 90.
Distance
Section titled “Distance”point:Distance(other: Vector2): numberReturns the distance between two points. The result between (0, 0) and (3, 4) is 5.
Common mistakes
Section titled “Common mistakes”Expecting the source vector to change
Section titled “Expecting the source vector to change”Store the result of Normalize():
direction = direction:Normalize()Calling the method without assignment does not change direction.