Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


Vector4

Representation of four-dimensional vectors.
Read time 2 minutesLast updated 4 days ago

Definition

public struct Vector4 : IEquatable<Vector4>, IFormattable

Remarks

This structure is used in some places to represent four component vectors (e.g. mesh tangents, parameters for shaders). In the majority of other cases a Vector3 is used.

Fields

Value

Description

wW component of the vector.
xX component of the vector.
yY component of the vector.
zZ component of the vector.

Constructors

Constructor

Description

Vector4(System.Single,System.Single)Creates a new vector with given x, y components and sets
z
and
w
to zero.
Vector4(System.Single,System.Single,System.Single)Creates a new vector with given x, y, z components and sets
w
to zero.
Vector4(System.Single,System.Single,System.Single,System.Single)Creates a new vector with given x, y, z, w components.

Properties

Property

Description

this[]Access the x, y, z, w components using [0], [1], [2], [3] respectively.
magnitudeReturns the length of this vector (Read Only).
normalizedCreates a normalized copy of a vector based on the current vector.
sqrMagnitudeReturns the squared length of this vector (Read Only).

Static Properties

Property

Description

negativeInfinityShorthand for writing
Vector4(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity)
.
oneShorthand for writing
Vector4(1,1,1,1)
.
positiveInfinityShorthand for writing
Vector4(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity)
.
zeroShorthand for writing
Vector4(0,0,0,0)
.

Methods

Method

Description

EqualsCompares two vectors to each other and evaluates if they are equal.
NormalizeMakes this vector have a Vector4.magnitude of 1.
ScaleMultiplies every component of this vector by the same component of
scale
.
SetSet x, y, z and w components of an existing Vector4.
ToStringFormats this vector as a string.

Static Methods

Method

Description

DistanceReturns the distance between a and b.
DotCalculates the dot product of two vectors.
LerpLinearly interpolates between two vectors.
LerpUnclampedLinearly interpolates between two vectors.
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
.
NormalizeReturns a normalized vector based on the given vector. The normalized vector has a Vector4.magnitude of 1 and is in the same direction as the given vector. Returns a zero vector if the given vector is too small to be normalized.
ProjectProjects a vector onto another vector.
ScaleMultiplies two vectors component-wise.

Operators

Operator

Description

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