Skip to main content
Vector is a 3D vector with x, y, z components. World positions, velocities, normals, trace endpoints - almost everything spatial is a Vector. If you’re writing game logic, you’ll use this type more than any other.
All Vector arithmetic methods return a new Vector - the original is never modified. The exceptions are normalize() and direct property assignment (vec.x = 10), which mutate in place.

Overview

Creating vectors

Constructor and static factories.

Arithmetic

Add, subtract, multiply, scale - all return new instances.

Length & distance

Magnitude, squared variants, and distance between points.

Products

Dot product, cross product.

Normalization

Unit vectors - mutating and non-mutating.

Utility

Clone, validity checks, conversion, string output.

Properties


Creating vectors

Constructor


Creates a new Vector from three components.

Vector.zero


Returns a new (0, 0, 0) vector.

Vector.one


Returns a new (1, 1, 1) vector. Useful as a default scale or as a starting multiplier.

Arithmetic

All arithmetic methods return a new Vector. The original is never modified.

add


Component-wise addition. Returns a new Vector.

sub


Component-wise subtraction. Returns a new Vector.

mul


Component-wise multiplication - x*x, y*y, z*z. Returns a new Vector. This is not dot or cross product - it’s the Hadamard product.
Use mul for component-wise operations like applying a per-axis scale factor. For scalar multiplication use scale, for the geometric product use dot or cross.

scale


Multiplies all three components by a scalar. Returns a new Vector.

Length & distance

length


Returns the Euclidean length: √(x² + y² + z²).

lengthSqr


Returns the squared length: x² + y² + z². Faster than length() because it skips the square root - use it when you’re comparing magnitudes, not when you need the actual value.
When to use lengthSqr vs length:

length2D


Returns the length on the XY plane only: √(x² + y²). Ignores the vertical component. Useful for horizontal speed or ground-plane distance.

distTo


Returns the Euclidean distance to another vector. Equivalent to vec.sub(other).length() but slightly faster.

distToSqr


Returns the squared distance to another vector. Same speed advantage as lengthSqr - no square root.

Products

dot


Returns the dot product: x*ox + y*oy + z*oz. Measures how much two vectors point in the same direction. Returns number:
  • Positive → vectors point roughly the same way
  • 0 → perpendicular
  • Negative → point away from each other
dot on two unit vectors gives the cosine of the angle between them. alignment > 0.9 ≈ within ~25°, > 0.95 ≈ within ~18°.

cross


Returns the cross product - a vector perpendicular to both inputs. The magnitude equals the area of the parallelogram they span.
Cross product is not commutative: a.cross(b) and b.cross(a) point in opposite directions. Order matters.

Normalization

normalize


Scales the vector to length 1 in place. Returns this.
Mutates the original. If you need the raw vector preserved, use normalized() instead.

normalized


Returns a new unit vector pointing in the same direction. The original is untouched.
normalize() vs normalized() - same pattern as QAngle:Use normalized() when you still need the original (e.g. to get distance from its length). Use normalize() when you’re done with the magnitude and just want the direction.

Utility

isZero


Returns true if all components are within epsilon of zero. Default epsilon is 1e-6.

isFinite


Returns true if all three components are finite numbers (not NaN, not ±Infinity).

clone


Returns an independent deep copy. Modifying the clone does not affect the original.

toArray


Returns [x, y, z] as a plain array.

toString


Returns a human-readable string.

Static methods

Vector.lerp


Linear interpolation between two vectors. t=0 returns a, t=1 returns b, t=0.5 returns the midpoint. Not clamped - t outside [0, 1] extrapolates.
Vector.lerp with a constant t each frame gives exponential decay - the same smooth-follow behavior as Math.lerp. Values around 0.1–0.3 feel smooth, 0.5+ feels snappy.

Vector.min


Returns a vector with the minimum of each component pair: (min(a.x, b.x), min(a.y, b.y), min(a.z, b.z)).

Vector.max


Returns a vector with the maximum of each component pair: (max(a.x, b.x), max(a.y, b.y), max(a.z, b.z)).

Common patterns

Real-world patterns that combine Vector methods with the Math API.