Vectors
Summary
A vector is a set of numbers grouped together to represent both a
magnitude and a direction — in games, most often used to represent a
position, a movement, or a direction in 2D or 3D space. A 2D vector (3, 4) and a 3D vector (3, 4, 0) are both just "an amount of X, an amount of
Y, (an amount of Z)" bundled into one value, manipulated as a single unit
rather than as separate loose numbers.
Why This Matters
Position, movement, and direction all fundamentally need more than one
number at once — a 2D position needs both an X and a Y, and treating them
as two completely separate variables (playerX, playerY) makes even
simple operations (move the player, find the distance to a target) require
manually repeating the same arithmetic on each axis separately. Vectors
bundle those related numbers into a single value with its own arithmetic
rules, so playerPosition + movement correctly updates both axes at once,
and every later concept in this track (magnitude, normalization, dot
product) builds directly on treating position and direction this way.
Mental Model
A vector is an arrow, not a dot. A single point on a page shows where
something is, but says nothing about motion or direction. An arrow drawn
from one point to another shows both — how far, and which way. A vector
(3, 4) is exactly that arrow: it can represent "3 right and 4 up from
here," a specific offset with both a length and a direction baked into the
same pair of numbers.
Core Explanation
- A vector groups related numbers into one value.
Vector2(x, y)orVector3(x, y, z)are structs holding their components together, with operations (addition, scaling) defined to act on all components at once. - Vectors are used for more than one purpose in games — position,
direction, and velocity all use the same underlying type. A
Vector3representing "where the player is" and aVector3representing "which way the player is facing" are the same data structure used for two different meanings — the context, not the type, determines what it represents. - Component-wise addition and subtraction combine vectors
intuitively.
(3, 4) + (1, 1) = (4, 5)— each component adds independently; subtracting one position from another produces the vector pointing from one to the other. - Scaling a vector by a plain number changes its length, not its
direction.
(3, 4) * 2 = (6, 8)points the same way as(3, 4), just twice as far — multiplying by a fraction shrinks it, multiplying by a negative number reverses it.
Syntax / Theory
Engine-neutral (concept)
Vector2(x, y) — two components, used for 2D position/direction.
Vector3(x, y, z) — three components, used for 3D position/direction.
Addition: (a, b) + (c, d) = (a+c, b+d)
Subtraction: (a, b) - (c, d) = (a-c, b-d)
Scaling: (a, b) * k = (a*k, b*k)
C# (Unity)
Vector3 playerPosition = new Vector3(0, 0, 0);
Vector3 movement = new Vector3(1, 0, 0);
playerPosition += movement * speed * Time.deltaTime; // Position updated as one operation.
C++ (Unreal)
FVector PlayerPosition(0.f, 0.f, 0.f);
FVector Movement(1.f, 0.f, 0.f);
PlayerPosition += Movement * Speed * DeltaTime; // Identical shape to the C# version.
Both engines use a dedicated vector type (Vector3 in Unity, FVector in
Unreal) with overloaded +, -, and * operators, so vector arithmetic
reads exactly like ordinary arithmetic despite operating on three numbers
at once.
Practical Example
Finding the offset between two points is a single vector subtraction, rather than two separate per-axis subtractions handled manually.
Vector3 targetPosition = new Vector3(10, 0, 5);
Vector3 playerPosition = new Vector3(2, 0, 1);
Vector3 offset = targetPosition - playerPosition; // (8, 0, 4) — one operation, both axes.
offset is itself a vector — it has both a length (how far away the
target is) and a direction (which way to go), both derivable from this
single subtraction (see [[CON-0044]] and [[CON-0047]]).
Common Mistakes
- Manually manipulating individual X/Y/Z fields when a single vector
operation would do the same thing more clearly. Writing
pos.x += move.x; pos.y += move.y; pos.z += move.z;instead ofpos += move;is more error-prone (easy to forget one axis) and harder to read. - Confusing a vector representing a position with one representing a
direction or offset.
Vector3(5, 0, 0)might be "the point at X=5" or "move 5 units along X" depending on context — mixing the two up (adding two positions together instead of a position and an offset) produces nonsensical results. - Forgetting that scaling a vector changes its length, not its
direction — except when scaling by zero or a negative number.
Scaling by
0collapses the vector to nothing (no direction at all); scaling by a negative number flips it to point the opposite way. - Treating a 2D and 3D vector as interchangeable without deliberately
deciding which axis is being dropped or added. Converting a
Vector3(x, y, z)to aVector2by dropping the wrong axis (e.g. droppingy, "up," when a 2D top-down game actually needs to keepxandz) silently produces incorrect 2D positions.
Game Development Example
A simple top-down movement script builds a movement vector from input and applies it directly to the position.
public class TopDownMover : MonoBehaviour
{
public float speed = 5f;
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(h, 0, v); // A direction, built from two input axes.
transform.position += movement * speed * Time.deltaTime;
}
}
The player can move diagonally, in any combination of directions, without any special-case code — the vector naturally represents "a bit of X, a bit of Z" together, exactly matching how a joystick or WASD diagonal input should feel.
Best Practices
- Use the engine's built-in vector type and operators rather than manipulating individual axis fields separately.
- Be deliberate and consistent about what a given vector represents
(position, direction, velocity) — name variables accordingly
(
targetPosition,moveDirection) rather than a genericvec. - When converting between 2D and 3D, explicitly decide which axis is being added or dropped, and double-check it matches the coordinate system's "up" convention.
- Reach for vector subtraction to find the offset or direction between two points, rather than computing each axis difference manually.
Exercises
- Given two positions,
A = (2, 0, 3)andB = (7, 0, 8), computeB - Aby hand, then verify it in code with aVector3subtraction. - Take the
TopDownMoverexample and add aVector3.upcomponent to make the object also rise while moving — explain what changed about the resulting direction and why. - You're building an inventory drag-and-drop UI system entirely in 2D
screen space. Decide whether to use
Vector2orVector3for cursor and slot positions, and justify the choice given the system doesn't need any depth information. - Research how a vector differs from a "point" conceptually in
mathematics (a vector has no fixed location, only magnitude and
direction, while a point does), and explain why game engines still
commonly use the same
Vector3type to represent both.
Related Concepts
- Vector Magnitude — the length of a vector, one of the two properties (the other being direction) that give a vector its meaning.
- Vector Normalization — converting a vector to a consistent length of 1 while preserving its direction, a common operation once a vector's basic arithmetic is understood.
- Direction Vectors — the specific, common use of a vector to represent "which way," typically derived from subtracting two positions as shown in the Practical Example.
Further Reading
KNOWLEDGE_SCHEMA.md— GDKB's concept maturity ladder and cross-linking rules, which this document follows.- Unity documentation — Vector3: https://docs.unity3d.com/ScriptReference/Vector3.html
- Unreal Engine documentation — FVector: https://dev.epicgames.com/documentation/en-us/unreal-engine/API/Runtime/Core/Math/FVector