Lesson 3 of 7

3D math you actually need: vectors, distance, CFrame

The 3D math behind Roblox games, practised in runnable cells and through Beacon Yard's numbers: Vector3 offsets, distance with Magnitude, direction with Unit, dot products for facing checks, CFrame position and rotation, and lerp.

Concept lesson, about 70 minutes, 10 steps

By the end you can

  • Subtract positions to get an offset, and use Magnitude and Unit for distance and direction
  • Use a dot product to tell whether something is in front, beside or behind
  • Read and build a CFrame, apply it to an offset, and blend between points with lerp

Before this: Parts, models, materials and pivots

Step 1 of 10

Twelve studs

Later in Beacon Yard, one line inside CoinService decides whether you get paid. A touch event says your foot brushed the coin. The server does not trust that, so it measures:

		-- A touch is only a hint. Check the character's root is close
		-- enough to the coin before paying.
		local character = player.Character
		local rootPart = if character then character:FindFirstChild("HumanoidRootPart") else nil
		if rootPart == nil or not rootPart:IsA("BasePart") then
			return
		end
		if (rootPart.Position - coin.Position).Magnitude > 12 then

rootPart.Position - coin.Position is a vector: an arrow from the coin to your character. .Magnitude is that arrow's length in studs. More than 12, no coins. That one line uses most of the math in this lesson. You will practise every piece right here in runnable Vector3 and CFrame cells, without needing to know Studio scripting yet.

Choose one

What does the number 12 in CoinService's check measure?

What does the number 12 in CoinService's check measure?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 2 of 10

A Vector3 is a walk

A Vector3Vector3Three numbers, X, Y and Z, used for positions, sizes and directions.Think of it as walking directions: so many studs along X, up and along Z. is three numbers: X, Y and Z. Y is up. In the yard, X runs east and west, Z runs north and south. Vector3.new(3, 0, 4) can mean "walk 3 studs along X and 4 along Z". It can also mean a place: "the point 3 along X and 4 along Z from the origin".

Both sidesVector3 is walk 3 east, 4 north.
In the pictureIn Roblox
A set of walking directionsA Vector3 offset
How far the walk isMagnitude
Which way to start walkingUnit

See the whole analogy cast

A Vector3 is a set of walking directions: so many studs along X, up and along Z. Add two and you follow one, then the other. Subtract a start from an end and you get the directions from the start to the end.

Think of it as Walk 3 east, 4 north

The rule you use most is subtraction. target - origin is the walk from origin to target. Order matters: origin - target is the same walk backwards.

Luau with roblox-lite (not Roblox)
Ctrl Enter runs the cell. Checked against the expected output

Both sidesRemember: Vector3 is walk 3 east, 4 north.

Predict the output

The shop is at (28, 3, 28) and the beacon at (0, 1.5, 0). What does this print?

local shop = Vector3.new(28, 3, 28)
local beacon = Vector3.new(0, 1.5, 0)
print(beacon - shop)
The shop is at (28, 3, 28) and the beacon at (0, 1.5, 0). What does this print?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 3 of 10

Distance is Magnitude

The length of a walk is its MagnitudeMagnitudeThe length of a vector: the square root of X squared plus Y squared plus Z squared.Think of it as how long the walk is.: the square root of X² + Y² + Z², which is Pythagoras in three dimensions1. A walk of (3, 0, 4) is 5 studs long, not 7. That is why checking X and Z separately is a bug: a player 9 studs along X and 9 along Z passes both "under 12" checks, but is 12.7 studs away.

Luau with roblox-lite (not Roblox)
Ctrl Enter runs the cell. Checked against the expected output

Ways to ask "is it close enough?"

OptionWhat it isWhen pros pick itTrade-offs
Magnitude check(a - b).Magnitude <= range, one subtraction and a square root.A handful of checks: one coin, one drone, one prompt. What Beacon Yard does.Checking every player against every object each frame grows fast.
Squared distanceCompare the squared length with range * range and skip the square root.Tight loops over many objects, in code that runs every frame.A small speed-up that makes the code harder to read; measure before you bother.
Spatial queryworkspace:GetPartBoundsInRadius(point, radius, params) asks the engine for parts near a point.Many objects, or you do not know in advance which ones to test.Returns parts, not players; filter the results, and it costs more per call than one subtraction.
TouchedLet physics report contact.Cheap hints that something arrived, such as stepping on a pad.Fires many times, not from scripted movement, and a client can fake its own contacts; always confirm on the server.
Work it out

A player stands 9 studs along X and 9 studs along Z from a coin, at the same height. How far away is the player, to one decimal place?

studs

Link to this step · This confused me

Step 4 of 10

Direction is Unit

Sometimes you want the way to go, not how far. .Unit keeps a vector's direction and makes its length exactly 1. Multiply a unit vector by a number and you get a walk of that many studs in that direction. When you build the wrench's raycast later in The wrench: tools, raycasts, tags, direction.Unit * 14 uses this exact trick to cap the reach at 14 studs.

The drone at m06-drone-wrench uses .Unit every frame too. It flattens the offset to the nearest player onto the ground (Y = 0, so it never dives), takes the unit direction, and moves DRONE_SPEED * dt studs along it, never closer than STOP_DISTANCE:

local function moveDrone(drone: Drone, dt: number)
	local root = drone.root
	local pos = Vector3.new(root.Position.X, HOVER_Y, root.Position.Z)
	local rotation = root.CFrame.Rotation
	local target = nearestLivingRoot(root.Position)
	if target then
		local flat = Vector3.new(target.Position.X - pos.X, 0, target.Position.Z - pos.Z)
		local distance = flat.Magnitude
		if distance > STOP_DISTANCE then
			local travel = math.min(Config.DRONE_SPEED * dt, distance - STOP_DISTANCE)
			local direction = flat.Unit
			pos += direction * travel
			rotation = CFrame.lookAt(Vector3.zero, direction).Rotation
		end
	end
	drone.model:PivotTo(CFrame.new(pos) * rotation)

One trap: a zero vector has no direction. .Unit of (0, 0, 0) divides by zero and gives NaN, "not a number", which then spreads into every position it touches. The drone avoids it because it only moves when the distance is greater than 3.

The vector playground

Goal Put the player exactly 12.00 studs from the coin on a diagonal, then step the drone until it stops 3 studs short.

The vector playground: a preview of the simulation

Drag the player, the coin and the drone. The readout shows the offset, its Magnitude, its Unit vector and the dot product with the player's facing.

Link to this step · This confused me

Step 5 of 10

In front or behind: the dot product

The dot productDot productOne number, from -1 to 1 for unit vectors, that says how much two directions agree.Think of it as a check on whether two arrows point the same way. of two unit vectors is one number between −1 and 1 that says how much they point the same way: 1 means the same direction, 0 means at right angles, −1 means opposite. It is the cosine of the angle between them.

That makes a cheap facing check. Take where a player is looking (LookVector, a unit vector), the unit direction to a target, and dot them. Above 0.5, the target is within 60° of straight ahead.

Luau with roblox-lite (not Roblox)
Ctrl Enter runs the cell. Checked against the expected output
Choose one

A facing check computes look:Dot((drone - player).Unit) and gets −0.9. What does that tell you?

A facing check computes look:Dot((drone - player).Unit) and gets −0.9. What does that tell you?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 6 of 10

CFrame: a position and a rotation

A part's position is a Vector3, but where it faces needs more. A CFrameCFrameA coordinate frame: a position and a rotation together.Think of it as a map pin with an arrow on it. ("coordinate frame") holds a position and a rotation together2:

  • CFrame.new(x, y, z) is a position with no rotation.
  • CFrame.Angles(rx, ry, rz) is a rotation, in radians. math.rad(90) turns degrees into radians.
  • CFrame.lookAt(from, to) sits at from and faces to.
  • cf.Position and cf.LookVector read it back. A CFrame with no rotation looks along −Z.
  • cf * offset turns an offset in the CFrame's own frame into a world position. "2 studs in front of the drone" is droneCFrame * Vector3.new(0, 0, -2).

The beacon's pedestal is a cylinder. Cylinders lie along X, so MapBuilder rolls it 90° on Z to stand it up: CFrame.new(0, 1.5, 0) * CFrame.Angles(0, 0, math.rad(90)). The drone turns to face where it is going with CFrame.lookAt(Vector3.zero, direction).

Both sidesCFrame is a map pin with an arrow.
In the pictureIn Roblox
The pinCFrame.Position
The arrow on the pinThe rotation, read as LookVector
Walk this far relative to the arrowcframe * offset

See the whole analogy cast

A CFrame is a map pin with an arrow on it: the pin says where, the arrow says which way. Multiplying by an offset means "from this pin, walk this far relative to the arrow".

Think of it as A map pin with an arrow

Luau with roblox-lite (not Roblox)
Ctrl Enter runs the cell. Checked against the expected output

Both sidesRemember: CFrame is a map pin with an arrow.

Choose one

Which direction does CFrame.new(0, 5, -45).LookVector point?

Which direction does CFrame.new(0, 5, -45).LookVector point?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 7 of 10

Where the pictures break

Both pictures in this lesson are useful and both have edges.

Where does it break?“Walk 3 east, 4 north” is a good picture of vector3. Where does it stop being true?

It breaks here. A Vector3 also stores a place: the same three numbers can be a walk from anywhere or a point measured from the origin, and only context says which.

The true model. A Vector3 is three numbers. As an offset it is a displacement; as a position it is the displacement from the origin. Subtracting positions gives the offset, and adding an offset to a position gives a new position.

Choose one

Pad1 sits at Position Vector3.new(22, 0, 0). Is that a point or a move of 22 studs from wherever the pad was?

Pad1 sits at Position Vector3.new(22, 0, 0). Is that a point or a move of 22 studs from wherever the pad was?

Press 1 to 2 to choose, Enter to check.

Where does it break?“A map pin with an arrow” is a good picture of cframe. Where does it stop being true?

It breaks here. A pin's arrow points one way on a flat map, but a CFrame rotates in all three dimensions, and multiplying CFrames in a different order gives a different result.

The true model. A CFrame stores a position and a 3 by 3 rotation matrix. Multiplying composes them: a * b applies b in a's frame. The order matters because rotations do not commute.

Choose one

Which line puts a part 5 studs in front of the drone, wherever the drone faces?

Which line puts a part 5 studs in front of the drone, wherever the drone faces?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 8 of 10

Lerp: part of the way there

LerpLerpLinear interpolation: the point a fraction t of the way from a to b.Think of it as part of the way there., short for linear interpolation, finds the point a fraction t of the way from a to b: a + (b - a) * t. At t = 0 you are at a, at 1 at b, at 0.5 exactly halfway. In Studio, a:Lerp(b, t) does it for Vector3 and cf:Lerp(other, t) for CFrames, which also blends the rotation.

Lerp and the drone's chase look similar but feel different. The drone moves a fixed 12 studs per second, so it arrives at a steady speed. Lerping a fixed fraction of the remaining distance every frame slows down as it gets close, which feels smooth for cameras and UI.

Luau with roblox-lite (not Roblox)
Ctrl Enter runs the cell. Checked against the expected output
Predict the output

What does this print?

local a = Vector3.new(0, 4, 18)
local b = Vector3.new(-30, 4, 0)
print(a + (b - a) * 0.5)
What does this print?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 9 of 10

Challenges

Level 1, Apprentice

Challenge

Without Studio, work out how far the spawn is from pad 1 and pad 4, and whether a player standing on the spawn could collect either coin.

Start from
Paper, or the Run cells above

Done means

  • You computed the distance from Spawn (−30, 3, 0) to each of Pad1 (22, 0, 0) and Pad4 (−22, 0, 0)
  • You said which pads are within 12 studs of the spawn

Meet the criteria above, then record your own result. No test grades this check.

Level 2, Builder

Challenge

Practice Vector3 and CFrame directly in the browser runner below. When every assertion passes, the cell completes this challenge.

Start from
The Run cell below
Check it here
Run the code cell; the challenge completes when it passes. No Studio needed.

Done means

  • inRange returns true when a and b are at most range studs apart
  • pointAhead uses cf * Vector3.new(0, 0, -distance) to place a point in front of cf
  • midpoint returns the point halfway between a and b
Luau with roblox-lite (not Roblox)
Ctrl Enter runs the cell. Checked by tests

Level 3, Professional

Challenge

Optional Studio stretch challenge: start from m14-ship and fill in the five functions in challenges/work/math_3d.luau. The : Vector3 and : number labels in that file are type annotations taught at the end of Module 2. The check is pure math. Press Play to start the Course Runner, then run this in the server command bar:

luau
game.ServerScriptService.RunGameTests:Invoke("math-3d")
Start from
The game at milestone m14 (download)
Grade it
In Studio, press Play, then run game.ServerScriptService.RunGameTests:Invoke("math-3d") in the server command bar. Copy the pass code from Output.

Done means

  • inRange, flatDirection, isFacing, chaseStep and lerp in challenges/work/math_3d.luau return the right values
  • flatDirection returns nil for points on top of each other instead of NaN
  • chaseStep stops 3 studs short and keeps the drone's own height

The Course Runner prints this code when the test passes.

Link to this step · This confused me

Step 10 of 10

Recap

Retiring the analogy

You no longer need “walk 3 east, 4 north”. A Vector3 is three numbers. As an offset it is a displacement; as a position it is the displacement from the origin. Subtracting positions gives the offset, and adding an offset to a position gives a new position.

Retiring the analogy

You no longer need “a map pin with an arrow”. A CFrame stores a position and a 3 by 3 rotation matrix. Multiplying composes them: a * b applies b in a's frame. The order matters because rotations do not commute.

Recap

  1. target - origin is the offset between two points; .Magnitude is its length in studs and .Unit its direction, never of a zero vector.
  2. The dot product of two unit vectors runs from 1 (same way) through 0 (sideways) to −1 (opposite): a cheap facing check.
  3. A CFrame is a position plus a rotation; cf * offset is relative to the CFrame and the order of multiplication matters. Lerp blends a + (b - a) * t.

Vector3 is walk 3 east, 4 north, until a Vector3 also stores a place: the same three numbers can be a walk from anywhere or a point measured from the origin, and only context says which.

CFrame is a map pin with an arrow, until a pin's arrow points one way on a flat map, but a CFrame rotates in all three dimensions, and multiplying CFrames in a different order gives a different result.

Next Building by hand: Terrain, solid modeling, Toolbox licensing, Packages

Link to this step · This confused me

Sources
  1. Vector3. Roblox Creator Documentation, checked 2026-11-23.
  2. CFrames. Roblox Creator Documentation, checked 2026-11-13.