0
0
Unityframework~10 mins

Waypoint systems in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Waypoint systems
Start
Set waypoints array
Set current waypoint index = 0
Move object towards current waypoint
Check if reached current waypoint?
NoContinue moving
Yes
Update current waypoint index to next
Loop back to move towards new waypoint
Repeat until all waypoints visited or loop
End or restart loop
The object moves step-by-step towards each waypoint in order, updating the target when it reaches one.
Execution Sample
Unity
Vector3[] waypoints = {A, B, C};
int current = 0;
while(true) {
  MoveTowards(waypoints[current]);
  if (Reached(waypoints[current])) current = (current + 1) % waypoints.Length;
}
Moves an object through points A, B, C in a loop, updating target when each is reached.
Execution Table
StepCurrent Waypoint IndexCurrent Target PositionObject PositionDistance to TargetAction
10A(0,0,0)5Move towards A
20A(1,0,0)4Move towards A
30A(3,0,0)2Move towards A
40A(5,0,0)0Reached A, update index to 1
51B(5,0,0)7Move towards B
61B(7,0,0)5Move towards B
71B(10,0,0)2Move towards B
81B(12,0,0)0Reached B, update index to 2
92C(12,0,0)4Move towards C
102C(14,0,0)2Move towards C
112C(16,0,0)0Reached C, update index to 0 (loop)
120A(16,0,0)11Move towards A again
💡 Loop continues indefinitely, cycling through waypoints A, B, C
Variable Tracker
VariableStartAfter Step 4After Step 8After Step 11After Step 12
current01200
Object Position(0,0,0)(5,0,0)(12,0,0)(16,0,0)(16,0,0)
Current TargetABCAA
Key Moments - 3 Insights
Why does the current waypoint index reset to 0 after reaching the last waypoint?
Because the code uses modulo (%) with the waypoints length to loop back, as shown in step 11 where index changes from 2 to 0 (execution_table row 11). This makes the movement repeat.
What happens if the object never exactly reaches the waypoint position?
The code checks if distance is zero to update index (execution_table column 'Distance to Target'). If never zero, the index won't update and the object keeps moving towards the same waypoint.
How does the object know which waypoint to move towards next?
The variable 'current' stores the index of the target waypoint and updates when the object reaches it (variable_tracker shows 'current' changes at steps 4, 8, 11).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 4. What is the current waypoint index after reaching waypoint A?
A1
B0
C2
D3
💡 Hint
Check the 'Action' column at step 4 where it says 'Reached A, update index to 1'
At which step does the object start moving towards waypoint C?
AStep 5
BStep 8
CStep 9
DStep 11
💡 Hint
Look at 'Current Target Position' column to see when it changes to C
If the object never reaches a waypoint, what happens to the 'current' index in variable_tracker?
AIt keeps increasing beyond waypoints length
BIt stays the same and does not update
CIt resets to 0 immediately
DIt becomes negative
💡 Hint
Refer to key moment 2 and execution_table 'Distance to Target' column
Concept Snapshot
Waypoint systems move an object through a list of points in order.
Use an index to track the current target waypoint.
Move the object towards the current waypoint each update.
When the object reaches the waypoint, update the index to the next.
Use modulo (%) to loop back to the first waypoint.
This creates smooth, repeatable path movement.
Full Transcript
This visual execution shows how a waypoint system works in Unity. We start with an array of waypoints and a current index set to zero. The object moves towards the waypoint at the current index. Each step, we check if the object has reached the waypoint by measuring distance. If reached, we update the index to the next waypoint, looping back to zero after the last. The execution table tracks the object's position, current target, and index changes step-by-step. Key moments clarify why the index loops and how the object knows which waypoint to move to next. The quiz tests understanding of index updates and movement flow. This helps beginners see how waypoint navigation works in a game or simulation.