Vector3 Operations

Download

Unity Cheat Sheet: #25

Abhishek Smith

Abhishek Smith

Building Outscal | Land Jobs in the gaming industry | EA, Epic Games, TCS, | CMU, IIT K

Vector3 in Unity represents a point or direction in 3D space using three values (x, y, z). Think of it like:

  • Position: Where something is in 3D space (5,0,2)
  • Direction: Which way it's pointing (-1,0,0 means left)
  • Force: How strongly to push in each direction (0,10,0 means upward force)

Core Vector Operations:

Basic Properties

  • x, y, z: Individual axis values
  • magnitude: Length of vector
  • normalized: Direction only (length of 1)
  • sqrMagnitude: Faster than magnitude for comparisons

Common Calculations

  • Dot: Angle between vectors (are objects facing each other?)
  • Cross: Perpendicular vector (create right-angle movements)
  • Distance: Space between points (is enemy in range?)
  • Reflect: Bounce direction (projectile rebounds)
  • Angle: Degrees between vectors (for rotations)
  • Lerp: Smooth movement between points
  • MoveTowards: Constant speed movement to target
  • RotateTowards: Constant speed rotation to target

Practical Uses:

Movement

  • position += direction * speed * Time.deltaTime
  • MoveTowards for consistent speed
  • Lerp for smooth acceleration/deceleration

Physics

  • AddForce for realistic push
  • Reflect for bouncing
  • Cross for perpendicular movement

Navigation

  • Distance for range checks
  • Dot for field of view
  • normalized for pure direction

Transformations

  • TransformPoint: Convert local to world space
  • InverseTransformPoint: World to local space
  • LookRotation: Make object face direction

Why Vector Math Matters:

  • Movement needs vectors
  • Physics uses vectors
  • AI pathfinding uses vectors
  • Camera control uses vectors
  • Animations use vectors

Show More