Api Vector3
struct

Vector3

public sealed struct Vector3

A 3-dimentional vector. Typically represents a position, size, or direction in 3D space.

Constructors

Vector3(Single x, Single y, Single z)

Initializes a vector with given components.

x — The X component.
y — The Y component.
z — The Z component.
Vector3(Single x, Single y)

Initializes a vector with given components and Z set to 0.

x — The X component.
y — The Y component.
Vector3(Vector3 other)

Initializes a Vector3 from a given Vector3, i.e. creating a copy.

Vector3(Vector2 other, Single z)

Initializes a Vector3 from given Vector2 and Z component.

Vector3(Single all)

Initializes the vector with all components set to given value.

Vector3(Vector3 v)

Properties

Name Type Description
x Single The X component of this vector.
y Single The Y component of this vector.
z Single The Z component of this vector.
Random static Vector3 Uniformly samples a 3D position from all points with distance at most 1 from the origin.
Normal Vector3 Returns a unit version of this vector. A unit vector has length of 1.
Length Single Length (or magnitude) of the vector (Distance from 0,0,0).
LengthSquared Single Squared length of the vector. This is faster than Length, and can be used for things like comparing distances, as long as only squared values are used.
Inverse Vector3 Returns the inverse of this vector, which is useful for scaling vectors.
IsNaN Boolean Returns true if x, y or z are NaN
IsInfinity Boolean Returns true if x, y or z are infinity
IsNearZeroLength Boolean Returns true if the squared length is less than 1e-8 (which is really near zero)
EulerAngles Angles The Euler angles of this direction vector.
Item Single

Methods

Vector3 WithX(Single x)

Returns this vector with given X component.

x — The override for X component.
returns — The new vector.
Vector3 WithY(Single y)

Returns this vector with given Y component.

y — The override for Y component.
returns — The new vector.
Vector3 WithZ(Single z)

Returns this vector with given Z component.

z — The override for Z component.
returns — The new vector.
Boolean IsNearlyZero(Single tolerance = 0.0001)

Returns true if value on every axis is less than tolerance away from zero

Vector3 ClampLength(Single maxLength)

Returns a vector whose length is limited to given maximum.

Vector3 ClampLength(Single minLength, Single maxLength)

Returns a vector whose length is limited between given minimum and maximum.

Vector3 Clamp(Vector3 otherMin, Vector3 otherMax)

Returns a vector each axis of which is clamped to between the 2 given vectors. Basically clamps a point to an Axis Aligned Bounding Box (AABB).

otherMin — The mins vector. Values on each axis should be smaller than those of the maxs vector. See Vector3.Sort.
otherMax — The maxs vector. Values on each axis should be bigger than those of the mins vector. See Vector3.Sort.
Vector3 Clamp(Single min, Single max)

Returns a vector each axis of which is clamped to given min and max values.

min — Minimum value for each axis.
max — Maximum value for each axis.
static Vector3 Clamp(Vector3 value, Vector3 min, Vector3 max)

Restricts a vector between a minimum and a maximum value.

value — The vector to restrict.
min — The mins vector. Values on each axis should be smaller than those of the maxs vector. See Vector3.Sort.
max — The maxs vector. Values on each axis should be bigger than those of the mins vector. See Vector3.Sort.
Vector3 ComponentMin(Vector3 other)

Returns a vector that has the minimum values on each axis between this vector and given vector.

static Vector3 Min(Vector3 a, Vector3 b)

Returns a vector that has the minimum values on each axis between the 2 given vectors.

Vector3 ComponentMax(Vector3 other)

Returns a vector that has the maximum values on each axis between this vector and given vector.

static Vector3 Max(Vector3 a, Vector3 b)

Returns a vector that has the maximum values on each axis between the 2 given vectors.

static Vector3 Lerp(Vector3 a, Vector3 b, Single frac, Boolean clamp = True)

Performs linear interpolation between 2 given vectors.

a — Vector A
b — Vector B
frac — Fraction, where 0 would return Vector A, 0.5 would return a point between the 2 vectors, and 1 would return Vector B.
clamp — Whether to clamp the fraction argument between [0,1]
Vector3 LerpTo(Vector3 target, Single frac, Boolean clamp = True)

Performs linear interpolation between this and given vectors.

target — Vector B
frac — Fraction, where 0 would return this, 0.5 would return a point between this and given vectors, and 1 would return the given vector.
clamp — Whether to clamp the fraction argument between [0,1]
static Vector3 Lerp(Vector3 a, Vector3 b, Vector3 frac, Boolean clamp = True)

Performs linear interpolation between 2 given vectors, with separate fraction for each vector component.

a — Vector A
b — Vector B
frac — Fraction for each axis, where 0 would return Vector A, 0.5 would return a point between the 2 vectors, and 1 would return Vector B.
clamp — Whether to clamp the fraction argument between [0,1] on each axis
Vector3 LerpTo(Vector3 target, Vector3 frac, Boolean clamp = True)

Performs linear interpolation between this and given vectors, with separate fraction for each vector component.

target — Vector B
frac — Fraction for each axis, where 0 would return this, 0.5 would return a point between this and given vectors, and 1 would return the given vector.
clamp — Whether to clamp the fraction argument between [0,1] on each axis
static Vector3 Slerp(Vector3 a, Vector3 b, Single frac, Boolean clamp = True)

Performs spherical linear interpolation (Slerp) between two vectors.

a — Starting vector (A).
b — Target vector (B).
frac — Interpolation fraction: 0 returns A, 1 returns B, and values in between provide intermediate results along the spherical path.
clamp — If true, clamps the fraction between 0 and 1.
returns — Interpolated vector along the spherical path.
Vector3 SlerpTo(Vector3 target, Single frac, Boolean clamp = True)

Performs spherical linear interpolation (Slerp) between this vector and a target vector.

target — The target vector to interpolate towards.
frac — Interpolation fraction: 0 returns this vector, 1 returns the target vector, and values in between provide intermediate results along the spherical path.
clamp — If true, clamps the fraction between 0 and 1.
returns — Interpolated vector along the spherical path.
static Single InverseLerp(Vector3 pos, Vector3 a, Vector3 b, Boolean clamp = True)

Given a position, and two other positions, calculate the inverse lerp position between those

static Vector3 Cross(Vector3 a, Vector3 b)

Returns the cross product of the 2 given vectors. If the given vectors are linearly independent, the resulting vector is perpendicular to them both, also known as a normal of a plane.

Vector3 Cross(Vector3 b)

Returns the cross product of this and the given vector. If this and the given vectors are linearly independent, the resulting vector is perpendicular to them both, also known as a normal of a plane.

static Single Dot(Vector3 a, Vector3 b)

Returns the scalar/dot product of the 2 given vectors.

Single Dot(Vector3 b)

Returns the scalar/dot product of this and the given vectors.

static Single DistanceBetween(Vector3 a, Vector3 b)

Returns distance between the 2 given vectors.

Single Distance(Vector3 target)

Returns distance between this vector to given vector.

static Single DistanceBetweenSquared(Vector3 a, Vector3 b)

Returns squared distance between the 2 given vectors. This is faster than DistanceBetween, and can be used for things like comparing distances, as long as only squared values are used.

Single DistanceSquared(Vector3 target)

Returns squared distance between this vector to given vector. This is faster than Distance, and can be used for things like comparing distances, as long as only squared values are used.

static Vector3 Direction(Vector3 from, Vector3 to)

Calculates the normalized direction vector from one point to another in 3D space.

from
to
Vector3 SubtractDirection(Vector3 direction, Single strength = 1)

Given a vector like 1,1,1 and direction 1,0,0, will return 0,1,1. This is useful for velocity collision type events, where you want to cancel out velocity based on a normal. For this to work properly, direction should be a normal, but you can scale how much you want to subtract by scaling the direction. Ie, passing in a direction with a length of 0.5 will remove half the direction.

Vector3 Approach(Single length, Single amount)

Returns a new vector whose length is closer to given target length by given amount.

length — Target length.
amount — How much to subtract or add.
Vector3 Abs()

Returns a new vector with all values positive. -5 becomes 5, etc.

static Vector3 Abs(Vector3 value)

Returns a new vector with all values positive. -5 becomes 5, etc.

static Vector3 Reflect(Vector3 direction, Vector3 normal)

Returns a reflected vector based on incoming direction and plane normal. Like a ray reflecting off of a mirror.

static Vector3 VectorPlaneProject(Vector3 v, Vector3 planeNormal)

Projects given vector on a plane defined by .

v — The vector to project.
planeNormal — Normal of a plane to project onto.
returns — The projected vector.
Vector3 ProjectOnNormal(Vector3 normal)

Projects this vector onto another vector. Basically extends the given normal/unit vector to be as long as necessary to make a right triangle (a triangle which has a 90 degree corner) between (0,0,0), this vector and the projected vector.

normal
returns — The projected vector.
static Void Sort(Vector3 min, Vector3 max)

Sort these two vectors into min and max. This doesn't just swap the vectors, it sorts each component. So that min will come out containing the minimum x, y and z values.

Boolean AlmostEqual(Vector3 v, Single delta = 0.0001)

Returns true if we're nearly equal to the passed vector.

v — The value to compare with
delta — The max difference between component values
returns — True if nearly equal
static Vector3 CubicBezier(Vector3 source, Vector3 target, Vector3 sourceTangent, Vector3 targetTangent, Single t)

Calculates position of a point on a cubic beizer curve at given fraction.

source — Point A of the curve in world space.
target — Point B of the curve in world space.
sourceTangent — Tangent for the Point A in world space.
targetTangent — Tangent for the Point B in world space.
t — How far along the path to get a point on. Range is 0 to 1, inclusive.
returns — The point on the curve
Vector3 SnapToGrid(Single gridSize, Boolean sx = True, Boolean sy = True, Boolean sz = True)

Snap to grid along any of the 3 axes.

static Single GetAngle(Vector3 v1, Vector3 v2)

Return the distance between the two direction vectors in degrees.

Single Angle(Vector3 other)

Return the distance between the two direction vectors in degrees.

static Angles VectorAngle(Vector3 vec)

Converts a direction vector to an angle.

Vector3 AddClamped(Vector3 toAdd, Single maxLength)

Try to add to this vector. If we're already over max then don't add. If we're over max when we add, clamp in that direction so we're not.

Vector3 RotateAround(Vector3 center, Rotation rot)

Rotate this vector around given point by given rotation and return the result as a new vector. See RotateAround(Vector3@,Rotation@) for similar method that also transforms rotation.

center — Point to rotate around.
rot — How much to rotate by. Single) can be useful.
returns — The rotated vector.
static override Vector3 Parse(String str, IFormatProvider provider)
static Vector3 Parse(String str)
static Boolean TryParse(String str, Vector3 result)
static override Boolean TryParse(String str, IFormatProvider provider, Vector3 result)

Given a string, try to convert this into a vector. Example input formats that work would be "1,1,1", "1;1;1", "[1 1 1]". This handles a bunch of different separators ( ' ', ',', ';', '\n', '\r' ). It also trims surrounding characters ('[', ']', ' ', '\n', '\r', '\t', '"').

Vector3 WithAcceleration(Vector3 target, Single acceleration)

Move to the target vector, by amount acceleration

Vector3 WithFriction(Single frictionAmount, Single stopSpeed = 140)

Apply an amount of friction to the current velocity.

static Vector3 CatmullRomSpline(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, Single t)

Calculates a point on a Catmull-Rom spline given four control points and a parameter t.

static Vector3 TcbSpline(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, Single tension, Single continuity, Single bias, Single u)

Calculates an interpolated point using the Kochanek-Bartels spline (TCB spline).

p0
p1
p2
p3
tension — Tension parameter which affects the sharpness at the control point. Positive values make the curve tighter, negative values make it rounder.
continuity — Continuity parameter which affects the continuity between segments. Positive values create smoother transitions, negative values can create corners.
bias — Bias parameter which affects the direction of the curve as it passes through the control point. Positive values bias the curve towards the next point, negative values towards the previous.
u — The interpolation parameter between 0 and 1, where 0 is the start of the segment and 1 is the end.
returns — The interpolated point on the curve.
static Vector3 SmoothDamp(Vector3 current, Vector3 target, Vector3 velocity, Single smoothTime, Single deltaTime)

Smoothly move towards the target vector

static Vector3 SpringDamp(Vector3 current, Vector3 target, Vector3 velocity, Single deltaTime, Single frequency = 2, Single damping = 0.5)

Springly move towards the target vector

static Vector3 SpringDamp(Vector3 current, Vector3 target, Vector3 velocity, Single smoothTime, Single deltaTime, Single frequency = 2, Single damping = 0.5)

Fields

Name Type Description
One static Vector3 A vector with all components set to 1.
Zero static Vector3 A vector with all components set to 0.
Forward static Vector3 A vector with X set to 1. This represents the forwards direction.
Backward static Vector3 A vector with X set to -1. This represents the backwards direction.
Up static Vector3 A vector with Z set to 1. This represents the upwards direction.
Down static Vector3 A vector with Z set to -1. This represents the downwards direction.
Right static Vector3 A vector with Y set to -1. This represents the right hand direction.
Left static Vector3 A vector with Y set to 1. This represents the left hand direction.
Assembly: Sandbox.System Full Name: Vector3