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

NameReturn TypeDescription

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

NameReturn TypeDescription

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:

NameTypeDefaultDescription

x

number

0

The x coordinate

y

number

0

The y coordinate

Example

newVector = Vector2(1, 2)

Vector2:Dot(a, b)

The dot product of two vectors

Returns: number

Parameters:

NameTypeDefaultDescription

a

The first vector

b

The second vector

Example

result = Vector3:Dot(myVector, otherVector

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:

NameTypeDefaultDescription

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

newPoint = Vector2:Lerp(pointA, PointB, 0.25)

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:

NameTypeDefaultDescription

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

newPoint = Vector2:Lerp(pointA, PointB, 0.25)

Vector2:Max(a, b)

Creates a vector made from the largest components of the inputs

Returns: Vector2

Parameters:

NameTypeDefaultDescription

a

The first vector

b

The second vector

Example

result = Vector2:Max(firstVector, secondVector

Vector2:Min(a, b)

Creates a vector made from the largest components of the inputs

Returns: Vector2

Parameters:

NameTypeDefaultDescription

a

The first vector

b

The second vector

Example

result = Vector2:Min(firstVector, secondVector

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:

NameTypeDefaultDescription

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

newPoint = Vector3:Slerp(pointA, PointB, 0.25)

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:

NameTypeDefaultDescription

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

newPoint = Vector3:SlerpUnclamped(pointA, PointB, 0.25)

Vector2:PointOnCircle(degrees)

Returns the point the given number of degrees around a circle with radius 1

Returns: Vector2

Parameters:

NameTypeDefaultDescription

degrees

number

The angle in degrees

Example

result = Vector2:PointOnCircle(45)

Instance Methods

vector2:Angle(other)

The unsigned angle in degrees between this vector and another

Returns: number

Parameters:

NameTypeDefaultDescription

other

The other vector

Example

angle = myVector:Angle(otherVector)

vector2:ClampMagnitude(maxLength)

Returns a vector with the same direction but with it's length clamped to a maximum

Returns: Vector2

Parameters:

NameTypeDefaultDescription

maxLength

number

The maximum length of the new vector

Example

newVector = myVector:ClampMagnitude

vector2:Distance(other)

The distance between two points

Returns: number

Parameters:

NameTypeDefaultDescription

other

The other vector

Example

distance = Vector2:Distance(firstPoint, secondPoint)

vector2:MoveTowards(target, maxDistanceDelta)

Moves a point towards a target point

Returns: Vector2

Parameters:

NameTypeDefaultDescription

target

The target point

maxDistanceDelta

number

The maximum distance to move

Example

newPoint = Vector2:MoveTowards(currentPoint, targetPoint, 0.25)

vector2:Reflect(normal)

Reflects a vector off the vector defined by a normal

Returns: Vector2

Parameters:

NameTypeDefaultDescription

normal

The normal vector

Example

newVector = myVector:Reflect(normalVector)

vector2:Scale(other)

Scales a vector by multiplying it's components by the components of another vector

Returns: Vector2

Parameters:

NameTypeDefaultDescription

other

The vector to scale by

Example

newVector = myVector:Scale(otherVector)

vector2:SignedAngle(other)

Returns the signed angle in degrees between this vector and another

Returns: number

Parameters:

NameTypeDefaultDescription

other

The other vector

Example

result = myVector:SignedAngle(otherVector

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

myVector3 = myVector2:OnX()

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

myVector3 = myVector2:OnY()

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

myVector3 = myVector2:OnZ()

vector2:Add(other)

Adds this vector to another

Returns: Vector2

Parameters:

NameTypeDefaultDescription

other

The other vector

Example

result = myVector:Add(otherVector)

vector2:Add(x, y)

Adds the given x and y values to this vector

Returns: Vector2

Parameters:

NameTypeDefaultDescription

x

number

The x value

y

number

The y value

Example

result = myVector:Add(2, 3)

vector2:Subtract(other)

Subtracts another vector from this one

Returns: Vector2

Parameters:

NameTypeDefaultDescription

other

The other vector

Example

result = myVector:Subtract(otherVector)

vector2:Subtract(x, y)

Subtracts the given x and y values from this vector

Returns: Vector2

Parameters:

NameTypeDefaultDescription

x

number

The x value

y

number

The y value

Example

result = myVector:Subtract(2, 3)

vector2:Multiply(value)

Multiplies a vector by a scalar value

Returns: Vector2

Parameters:

NameTypeDefaultDescription

value

number

The value to multiply by

Example

result = myVector:Multiply(2)

vector2:ScaleBy(other)

Multiplies this vector by another component-wise

Returns: Vector2

Parameters:

NameTypeDefaultDescription

other

The other vector

Example

result = myVector:ScaleBy(Vector2:New(2, 3)))

vector2:ScaleBy(x, y)

Multiplies each component of this vector by the given x and y values

Returns: Vector2

Parameters:

NameTypeDefaultDescription

x

number

The x value

y

number

The y value

Example

result = myVector:ScaleBy(2, 3)

vector2:Divide(value)

Divides this vector by another

Returns: Vector2

Parameters:

NameTypeDefaultDescription

value

number

The value to divide by

Example

result = myVector:Divide(2)

vector2:NotEquals(other)

Is this vector not equal to another?

Returns: boolean

Parameters:

NameTypeDefaultDescription

other

The other vector

Example

if myVector:NotEquals(Vector2.zero) then print("Vector is not zero") end

vector2:NotEquals(x, y)

Is this vector not equal to the given x and y values?

Returns: boolean

Parameters:

NameTypeDefaultDescription

x

number

The x value

y

number

The y value

Example

if myVector:NotEquals(1, 2) then print("Vector is not 1,2") end

Last updated