Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


Vector2

Representation of 2D vectors and points.
Read time 3 minutesLast updated 5 days ago

Definition

public struct Vector2 : IEquatable<Vector2>, IFormattable

Remarks

This structure is used in some places to represent 2D positions and vectors (e.g. texture coordinates in a Mesh or texture offsets in Material). In the majority of other cases a Vector3 is used.

Fields

Value

Description

xX component of the vector.
yY component of the vector.

Constructors

Constructor

Description

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

Properties

Property

Description

this[]Access the x or y component using [0] or [1] respectively.
magnitudeReturns the length of this vector (Read Only).
normalizedReturns a normalized vector based on the current vector. The normalized vector has a Vector2.magnitude of 1 and is in the same direction as the current vector. Returns a zero vector If the current vector is too small to be normalized.
sqrMagnitudeReturns the squared length of this vector (Read Only).

Static Properties

Property

Description

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

Methods

Method

Description

EqualsDetermines if the given vector is exactly equal to this vector.
NormalizeMakes this vector have a Vector2.magnitude of 1.
ScaleMultiplies every component of this vector by the same component of
scale
.
SetSet x and y components of an existing Vector2.
ToStringFormats this vector as a string.

Static Methods

Method

Description

AngleGets the unsigned angle in degrees between from and to.
ClampMagnitudeCreates a copy of a given vector, with its magnitude clamped to a maximum length.
DistanceReturns the distance between a and b.
DotCalculates the dot product of two vectors.
LerpLinearly interpolates between vectors
a
and
b
by
t
.
LerpUnclampedLinearly interpolates between vectors
a
and
b
by
t
.
MaxCreates a vector that is made from the largest components of two vectors.
MinCreates a vector that is made from the smallest components of two vectors.
MoveTowardsMoves a point
current
towards
target
.
PerpendicularReturns 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.
ReflectReflects a vector off the surface defined by a normal.
ScaleMultiplies two vectors component-wise.
SignedAngleGets the signed angle in degrees between from and to.
SmoothDampGradually changes a vector towards a desired goal over time.

Operators

Operator

Description

operator +Adds two vectors.
operator /Divides a vector by a number.
operator ==Returns true if two vectors are approximately equal.
Vector3Converts a Vector2 to a Vector3.
Vector2Converts a Vector3 to a Vector2.
operator *Multiplies a vector by a number.
operator -Subtracts one vector from another.