Skip to main content

Vector2

Representation of 2D vectors and points. Unity Vector2

Members

Properties

NameDescription
Vector2.magnitudeReturns the length of this vector (Read Only).
Vector2.normalizedReturns this vector with a magnitude of 1 (Read Only).
Vector2.sqrMagnitudeReturns the squared length of this vector (Read Only).
Vector2.xX component of the vector.
Vector2.yY component of the vector.

Static Properties

NameDescription
Vector2.downShorthand for writing Vector2(0, -1).
Vector2.leftShorthand for writing Vector2(-1, 0).
Vector2.negativeInfinityShorthand for writing Vector2(float.NegativeInfinity, float.NegativeInfinity).
Vector2.oneShorthand for writing Vector2(1, 1).
Vector2.positiveInfinityShorthand for writing Vector2(float.PositiveInfinity, float.PositiveInfinity).
Vector2.rightShorthand for writing Vector2(1, 0).
Vector2.upShorthand for writing Vector2(0, 1).
Vector2.zeroShorthand for writing Vector2(0, 0).

Constructor

NameDescription
Vector2(x, y)Constructs a new vector with given x, y components.

Methods

NameDescription
Vector2.Equals(other)Returns true if the given vector is exactly equal to this vector.
Vector2.Normalize()Makes this vector have a magnitude of 1.
Vector2.Set(newX, newY)Set x and y components of an existing Vector2.

Static Methods

NameDescription
Vector2.Angle(from, to)Returns the unsigned angle in degrees between from and to.
Vector2.ClampMagnitude(vector, maxLength)Returns a copy of vector with its magnitude clamped to maxLength.
Vector2.Distance(a, b)Returns the distance between a and b.
Vector2.Dot(lhs, rhs)Dot Product of two vectors.
Vector2.Lerp(a, b, t)Linearly interpolates between vectors a and b by t.
Vector2.LerpUnclamped(a, b, t)Linearly interpolates between vectors a and b by t.
Vector2.Max(lhs, rhs)Returns a vector that is made from the largest components of two vectors.
Vector2.Min(lhs, rhs)Returns a vector that is made from the smallest components of two vectors.
Vector2.MoveTowards(current, target, maxDistanceDelta)Moves a point current towards target.
Vector2.Perpendicular(inDirection)Returns the 2D vector perpendicular to this 2D vector. The result is always rotated 90-degrees in a counter-clockwise direction for a 2D coordinate system where the positive Y axis goes up.
Vector2.Reflect(inDirection, inNormal)Reflects a vector off the vector defined by a normal.
Vector2.Scale(a, b)Multiplies two vectors component-wise.
Vector2.SignedAngle(from, to)Returns the signed angle in degrees between from and to.

Operators

NameDescription
Operator (Vector2-Vector2)Subtracts one vector from another.

| Unary Operator (-Vector2) | Negates a vector. Each component in the result is negated. |

| Operator (Vector2*number) | Multiplies a vector by a number. |

| Operator (number*Vector2) | Multiplies a vector by a number. |

| Operator (Vector2*Vector2) | Multiplies a vector by another vector. Multiplies each component of lhs by its matching component from rhs. |

| Operator (Vector2/number) | Divides a vector by a number. |

| Operator (Vector2+Vector2) | Adds two vectors. Adds corresponding components together. |

| Operator (Vector2==Vector2) | Returns true if two vectors are approximately equal. To allow for floating point inaccuracies, the two vectors are considered equal if the magnitude of their difference is less than 1e-5. |

| Implicit Cast Operator (Vector3) | Converts a Vector2 to a Vector3. |