Skip to content

Math & vectors

All of Unity's math value types are available and behave the same way. You construct them with new, combine them with operators (+, -, *), and use the static helpers (Vector3.Distance, Mathf.Lerp, …).

Value types are shared references here

NexusScript has no value types, so a Vector3 you store in a field and hand to two places is the same object. In practice you write the usual a = a + b / transform.position = pos style and never notice — just don't assume an assigned vector is an independent copy. Reassign with a fresh new Vector3(...) if you need a distinct one.

Vectors

Vector3 a = new Vector3(1, 2, 3);
Vector3 b = Vector3.up;                 // (0,1,0)  — also: zero, one, forward, back, left, right, down
Vector3 sum = a + b;                    // operators: + - *(scalar) /(scalar)
float len = a.magnitude;                // length; sqrMagnitude is cheaper (no sqrt)
Vector3 dir = (target - transform.position).normalized;   // unit direction toward target
Static helper Does
Vector3.Distance(a, b) distance between two points
Vector3.Dot(a, b) dot product (facing test: > 0 = same side)
Vector3.Cross(a, b) perpendicular vector
Vector3.Lerp(a, b, t) blend a→b by t (0..1) — great for smooth motion
Vector3.Angle(a, b) angle between, in degrees
Vector3.Project · Reflect · Normalize projection / bounce / unit‑length

Vector2 (UI, 2D, input axes) and Vector4 (shader params, colors‑as‑vectors) have the matching members. Per‑axis read/write: v.x, v.y, v.z, v.w.

// Smoothly slide an object toward a target each frame
transform.position = Vector3.Lerp(transform.position, target, 5f * Time.deltaTime);

Quaternions (rotations)

Rotations are quaternions — don't add Euler angles directly; build a rotation and multiply.

Quaternion r = Quaternion.Euler(0, 90, 0);          // from yaw/pitch/roll degrees
Quaternion look = Quaternion.LookRotation(forward);  // face a direction
Quaternion spin = Quaternion.AngleAxis(45, Vector3.up); // 45° about an axis
transform.rotation = Quaternion.Slerp(transform.rotation, look, 3f * Time.deltaTime); // smooth turn
Vector3 angles = transform.rotation.eulerAngles;     // back to degrees if you need them
Quaternion.identity "no rotation"
Quaternion.Euler(x,y,z) from degrees
Quaternion.LookRotation(dir) face a direction
Quaternion.AngleAxis(deg, axis) rotate about an axis
Quaternion.Slerp(a, b, t) smooth interpolation between rotations
q * v rotate a vector; q1 * q2 compose rotations

Color

Color c = new Color(1f, 0.5f, 0f);       // r,g,b (0..1), optional alpha
Color faded = new Color(1, 0, 0, 0.3f);  // translucent red
Color mix = Color.Lerp(Color.red, Color.blue, t);
Named constants: red green blue white black yellow cyan magenta gray grey clear. Channels: .r .g .b .a.

Mathf — float math

Mathf.* mirrors System.Math but returns float. Math.* also compiles (it's aliased to Mathf).

float h = Mathf.Clamp(value, 0f, 100f);
float eased = Mathf.Lerp(from, to, t);          // t clamped 0..1
float wob = Mathf.Sin(Time.time * 2f);          // oscillate -1..1
int n = Mathf.RoundToInt(3.7f);                 // 4  (also FloorToInt / CeilToInt)
float pct = Mathf.InverseLerp(0f, 200f, score); // where score sits in 0..200 → 0..1
Group Members
Clamp / blend Clamp · Clamp01 · Lerp · LerpUnclamped · InverseLerp · Repeat
Rounding Floor · Ceil · Round · FloorToInt · CeilToInt · RoundToInt · Sign · Abs
Trig / pow Sin · Cos · Tan · Asin · Acos · Atan2 · Pow · Sqrt · Log · Log10
Constants PI · Infinity · Epsilon · Deg2Rad · Rad2Deg

Random

float f = Random.value;                  // 0..1
float r = Random.Range(0f, 10f);         // float range (max inclusive)
int i = Random.Range(0, 6);              // int range (max EXCLUSIVE) → 0..5, a die is +1
Vector3 p = Random.insideUnitSphere * 5f;// random point in a 5 m sphere
Quaternion q = Random.rotation;          // random orientation

Random is per‑client

Each player's client has its own random stream — Random.Range will give different results on each machine. For anything that must agree (loot, card shuffles, who‑wins), compute it on one authority (an NPC/server action or Storage‑backed seed) and share the result, don't let every client roll independently.

Matrix4x4, Rect & Bounds

Available for the cases that need them — usually you won't:

  • Matrix4x4TRS(pos, rot, scale), MultiplyPoint, MultiplyVector, Scale, Translate, identity, transpose, determinant. For custom transform math / projecting points.
  • Rect — 2D rectangle (x,y,width,height, Contains, Overlaps); used with UI/screen space.
  • Bounds — an axis‑aligned box (center, size, extents, Contains, Intersects, ClosestPoint). You get one from Collider.bounds or Renderer to test "is this point inside?".
Bounds box = col.GetComponent<Collider>().bounds;
if (box.Contains(Player.GetPosition(id))) { /* player is inside the volume */ }

→ Next: put these to work in GameObjects & Transforms and Physics.