0
0
Unityframework~10 mins

Pathfinding basics in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Pathfinding basics
Start Node
Check Neighbors
Calculate Costs
Choose Next Node
Is Goal Reached?
NoRepeat Check Neighbors
Yes
Build Path Back
End
This flow shows how pathfinding starts at a node, checks neighbors, calculates costs, chooses the next node, repeats until the goal is reached, then builds the path back.
Execution Sample
Unity
using System.Collections.Generic;
using UnityEngine;

Vector2Int start = new Vector2Int(0, 0);
Vector2Int goal = new Vector2Int(2, 2);
List<Vector2Int> openList = new List<Vector2Int> { start };
List<Vector2Int> closedList = new List<Vector2Int>();
while (openList.Count > 0) {
  Vector2Int current = openList[0];
  openList.RemoveAt(0);
  if (current == goal) {
    break;
  }
  // Add unvisited neighbors to openList (naive BFS example)
}
This simple loop picks nodes from openList using RemoveAt(0) until it finds the goal. In Unity, Vector2Int represents grid positions for 2D pathfinding.
Execution Table
StepopenListclosedListcurrent NodeActionOutput
1[(0,0)][](0,0)Pop (0,0), check if goalNot goal, add neighbors (0,1),(1,0) to openList
2[(0,1),(1,0)][(0,0)](0,1)Pop (0,1), check if goalNot goal, add neighbors (0,2),(1,1)
3[(1,0),(0,2),(1,1)][(0,0),(0,1)](1,0)Pop (1,0), check if goalNot goal, add neighbors (2,0),(1,1)
4[(0,2),(1,1),(2,0)][(0,0),(0,1),(1,0)](0,2)Pop (0,2), check if goalNot goal, add neighbors (1,2)
5[(1,1),(2,0),(1,2)][(0,0),(0,1),(1,0),(0,2)](1,1)Pop (1,1), check if goalNot goal, add neighbors (2,1),(1,2),(2,2)
6[(2,0),(1,2),(2,1),(1,2),(2,2)][(0,0),(0,1),(1,0),(0,2),(1,1)](2,0)Pop (2,0), check if goalNot goal, add neighbors (2,1)
7[(1,2),(2,1),(1,2),(2,2),(2,1)][(0,0),(0,1),(1,0),(0,2),(1,1),(2,0)](1,2)Pop (1,2), check if goalNot goal, add neighbors (2,2)
8[(2,1),(1,2),(2,2),(2,1),(2,2)][(0,0),(0,1),(1,0),(0,2),(1,1),(2,0),(1,2)](2,1)Pop (2,1), check if goalNot goal, add neighbors (2,2)
9[(1,2),(2,2),(2,1),(2,2),(2,2)][(0,0),(0,1),(1,0),(0,2),(1,1),(2,0),(1,2),(2,1)](1,2)Pop (1,2), check if goalAlready visited, skip
10[(2,2),(2,1),(2,2),(2,2)][(0,0),(0,1),(1,0),(0,2),(1,1),(2,0),(1,2),(2,1)](2,2)Pop (2,2), check if goalGoal reached!
11[][(0,0),(0,1),(1,0),(0,2),(1,1),(2,0),(1,2),(2,1),(2,2)]Build path back from goalPath found
💡 Goal node (2,2) reached at step 10, loop ends.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8After 9After 10Final
openList[(0,0)][(0,1),(1,0)][(1,0),(0,2),(1,1)][(0,2),(1,1),(2,0)][(1,1),(2,0),(1,2)][(2,0),(1,2),(2,1),(1,2),(2,2)][(1,2),(2,1),(1,2),(2,2),(2,1)][(2,1),(1,2),(2,2),(2,1),(2,2)][(1,2),(2,2),(2,1),(2,2),(2,2)][(2,2),(2,1),(2,2),(2,2)][][]
closedList[][(0,0)][(0,0),(0,1)][(0,0),(0,1),(1,0)][(0,0),(0,1),(1,0),(0,2)][(0,0),(0,1),(1,0),(0,2),(1,1)][(0,0),(0,1),(1,0),(0,2),(1,1),(2,0)][(0,0),(0,1),(1,0),(0,2),(1,1),(2,0),(1,2)][(0,0),(0,1),(1,0),(0,2),(1,1),(2,0),(1,2),(2,1)][(0,0),(0,1),(1,0),(0,2),(1,1),(2,0),(1,2),(2,1)][(0,0),(0,1),(1,0),(0,2),(1,1),(2,0),(1,2),(2,1),(2,2)][(0,0),(0,1),(1,0),(0,2),(1,1),(2,0),(1,2),(2,1),(2,2)]
Key Moments - 3 Insights
Why do some nodes appear multiple times in the openList?
Nodes can be added multiple times if found from different neighbors before being processed. The execution_table rows 5-9 show repeated nodes like (1,2) and (2,2). The algorithm skips duplicates when popped if already in closedList.
How does the algorithm know when to stop?
It stops when the current node equals the goal node, as shown in step 10 of the execution_table where (2,2) is popped and recognized as the goal.
What happens after the goal is found?
After reaching the goal, the algorithm builds the path back by following parent links (not shown in code), summarized in step 11 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What nodes are in the openList?
A[(0,2),(1,1),(2,0)]
B[(1,0),(0,2),(1,1)]
C[(0,1),(1,0)]
D[(2,0),(1,2)]
💡 Hint
Check the openList column at step 4 in the execution_table.
At which step does the algorithm first add the goal node (2,2) to the openList?
AStep 7
BStep 6
CStep 5
DStep 10
💡 Hint
Look at the openList column and see when (2,2) first appears.
If the goal was at (3,3), what would happen to the execution_table?
AThe openList would never add neighbors.
BThe loop would continue until (3,3) is reached or openList is empty.
CThe algorithm would stop immediately at step 1.
DThe closedList would remain empty.
💡 Hint
Consider the exit_note and how the loop ends only when the goal is reached.
Concept Snapshot
Pathfinding basics in Unity:
- Use Vector2Int for grid node positions.
- Maintain openList (nodes to explore) and closedList (visited).
- Loop: dequeue current, check goal, enqueue unvisited neighbors.
- Repeat until goal found or no nodes left.
- Reconstruct path using parents.
Full Transcript
This visual execution shows how a pathfinding algorithm works step-by-step in Unity context. It starts at the start node, keeps track of nodes to explore in an open list, and nodes already checked in a closed list. Each step dequeues a node from the open list, checks if it is the goal, and if not, adds its neighbors to the open list (skipping visited). This repeats until the goal node is found. Then the path is built back from the goal using parent pointers. The tables track the open and closed lists, current node, and actions at each step. Key moments explain duplicates in openList and stopping condition. The quiz tests understanding of list contents and conditions. Ideal for beginners learning grid-based pathfinding like BFS or A* foundations.