Vector2
Summary
A position or offset in 2D space. See https://docs.unity3d.com/ScriptReference/Vector2.html for more detail on many of these methods or properties
Class Properties
Name | Return Type | Description |
---|---|---|
down | Vector2 Read-only | A vector of -1 in the y axis |
left | Vector2 Read-only | A vector of -1 in the x axis |
negativeInfinity | Vector2 Read-only | A vector of negative infinity in all axes |
one | Vector2 Read-only | A vector of 1 in all axes |
positiveInfinity | Vector2 Read-only | A vector of positive infinity in all axes |
right | Vector2 Read-only | A vector of 1 in the x axis |
up | Vector2 Read-only | A vector of 1 in the y axis |
zero | Vector2 Read-only | A vector of 0 in all axes |
Instance Properties
Name | Return Type | Description |
---|---|---|
this[index] | number Read/Write | The component at the specified index |
x | number Read/Write | The x coordinate |
y | number Read/Write | The y coordinate |
magnitude | number Read/Write | The length of this vector |
sqrMagnitude | number Read/Write | The square of the length of this vector (faster to calculate if you're just comparing two lengths) |
normalized | Vector2 Read-only | Returns a vector with the same distance but witha length of 1 |
Class Methods
Vector2:New(x, y)
Creates a new vector
Returns: Vector2
Parameters:
Name | Type | Default | Description |
---|---|---|---|
x | number | 0 | The x coordinate |
y | number | 0 | The y coordinate |
Example
Vector2:Dot(a, b)
The dot product of two vectors
Returns: number
Parameters:
Example
Vector2:Lerp(a, b, t)
Linearly interpolates between two points
Returns: Vector2 (A point somewhere between a and b based on the value of t)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
a | The first point | ||
b | The second point | ||
t | number | The value between 0 and 1 that controls how far between a and b the new point is |
Example
Vector2:LerpUnclamped(a, b, t)
Linearly interpolates (or extrapolates) between two points
Returns: Vector2 (A point somewhere between a and b based on the value of t)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
a | The first point | ||
b | The second point | ||
t | number | The value that controls how far between (or beyond) a and b the new point is |
Example
Vector2:Max(a, b)
Creates a vector made from the largest components of the inputs
Returns: Vector2
Parameters:
Example
Vector2:Min(a, b)
Creates a vector made from the largest components of the inputs
Returns: Vector2
Parameters:
Example
Vector2:Slerp(a, b, t)
Spherically interpolates between two vectors
Returns: Vector2 (A point somewhere between a and b based on the value of t)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
a | The first point | ||
b | The second point | ||
t | number | The value that controls how far between (or beyond) a and b the new point is |
Example
Vector2:SlerpUnclamped(a, b, t)
Spherically interpolates (or extrapolates) between two vectors
Returns: Vector2 (A point somewhere between a and b based on the value of t)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
a | The first point | ||
b | The second point | ||
t | number | The value that controls how far between (or beyond) a and b the new point is |
Example
Vector2:PointOnCircle(degrees)
Returns the point the given number of degrees around a circle with radius 1
Returns: Vector2
Parameters:
Name | Type | Default | Description |
---|---|---|---|
degrees | number | The angle in degrees |
Example
Instance Methods
vector2:Angle(other)
The unsigned angle in degrees between this vector and another
Returns: number
Parameters:
Name | Type | Default | Description |
---|---|---|---|
other | The other vector |
Example
vector2:ClampMagnitude(maxLength)
Returns a vector with the same direction but with it's length clamped to a maximum
Returns: Vector2
Parameters:
Name | Type | Default | Description |
---|---|---|---|
maxLength | number | The maximum length of the new vector |
Example
vector2:Distance(other)
The distance between two points
Returns: number
Parameters:
Name | Type | Default | Description |
---|---|---|---|
other | The other vector |
Example
vector2:MoveTowards(target, maxDistanceDelta)
Moves a point towards a target point
Returns: Vector2
Parameters:
Name | Type | Default | Description |
---|---|---|---|
target | The target point | ||
maxDistanceDelta | number | The maximum distance to move |
Example
vector2:Reflect(normal)
Reflects a vector off the vector defined by a normal
Returns: Vector2
Parameters:
Name | Type | Default | Description |
---|---|---|---|
normal | The normal vector |
Example
vector2:Scale(other)
Scales a vector by multiplying it's components by the components of another vector
Returns: Vector2
Parameters:
Name | Type | Default | Description |
---|---|---|---|
other | The vector to scale by |
Example
vector2:SignedAngle(other)
Returns the signed angle in degrees between this vector and another
Returns: number
Parameters:
Name | Type | Default | Description |
---|---|---|---|
other | The other vector |
Example
vector2:OnX()
Converts this 2D vector to a 3D vector on the YZ plane)
Returns: Vector3 (A 3D Vector based on the input but with x as 0: (0, inX, inY))
Example
vector2:OnY()
Converts this 2D vector to a 3D vector on the XZ plane (i.e. with all y values set to 0)
Returns: Vector3 (A 3D Vector based on the input but with y as 0: (inX, 0, inY))
Example
vector2:OnZ()
Converts this 2D vector to a 3D vector on the XY plane (i.e. with all z values set to 0)
Returns: Vector3 (A 3D Vector based on the input but with z as 0: (inX, inX, 0))
Example
vector2:Add(other)
Adds this vector to another
Returns: Vector2
Parameters:
Name | Type | Default | Description |
---|---|---|---|
other | The other vector |
Example
vector2:Add(x, y)
Adds the given x and y values to this vector
Returns: Vector2
Parameters:
Name | Type | Default | Description |
---|---|---|---|
x | number | The x value | |
y | number | The y value |
Example
vector2:Subtract(other)
Subtracts another vector from this one
Returns: Vector2
Parameters:
Name | Type | Default | Description |
---|---|---|---|
other | The other vector |
Example
vector2:Subtract(x, y)
Subtracts the given x and y values from this vector
Returns: Vector2
Parameters:
Name | Type | Default | Description |
---|---|---|---|
x | number | The x value | |
y | number | The y value |
Example
vector2:Multiply(value)
Multiplies a vector by a scalar value
Returns: Vector2
Parameters:
Name | Type | Default | Description |
---|---|---|---|
value | number | The value to multiply by |
Example
vector2:ScaleBy(other)
Multiplies this vector by another component-wise
Returns: Vector2
Parameters:
Name | Type | Default | Description |
---|---|---|---|
other | The other vector |
Example
vector2:ScaleBy(x, y)
Multiplies each component of this vector by the given x and y values
Returns: Vector2
Parameters:
Name | Type | Default | Description |
---|---|---|---|
x | number | The x value | |
y | number | The y value |
Example
vector2:Divide(value)
Divides this vector by another
Returns: Vector2
Parameters:
Name | Type | Default | Description |
---|---|---|---|
value | number | The value to divide by |
Example
vector2:NotEquals(other)
Is this vector not equal to another?
Returns: boolean
Parameters:
Name | Type | Default | Description |
---|---|---|---|
other | The other vector |
Example
vector2:NotEquals(x, y)
Is this vector not equal to the given x and y values?
Returns: boolean
Parameters:
Name | Type | Default | Description |
---|---|---|---|
x | number | The x value | |
y | number | The y value |
Example
Last updated