0
0
Unityframework~10 mins

NavMesh Agent component in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - NavMesh Agent component
Start
Set Destination
Calculate Path on NavMesh
Move Agent Along Path
Check if Destination Reached
Continue Moving
Update Position
Repeat Until Destination Reached
The NavMesh Agent moves step-by-step towards a set destination by calculating a path on the NavMesh and updating its position until it reaches the target.
Execution Sample
Unity
NavMeshAgent agent;

void Start() {
  agent = GetComponent<NavMeshAgent>();
  agent.SetDestination(target.position);
}

void Update() {
  if (!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance) {
    agent.isStopped = true;
  }
}
This code sets a destination for the NavMesh Agent and stops it when it reaches close enough to the target.
Execution Table
StepAgent PositionDestinationPath CalculatedRemaining DistanceActionAgent State
1(0,0,0)(10,0,10)Path calculated14.14Start movingMoving
2(1,0,1)(10,0,10)Following path12.73Continue movingMoving
3(3,0,3)(10,0,10)Following path9.90Continue movingMoving
4(6,0,6)(10,0,10)Following path5.65Continue movingMoving
5(8,0,8)(10,0,10)Following path2.83Continue movingMoving
6(9.5,0,9.5)(10,0,10)Following path0.71Continue movingMoving
7(10,0,10)(10,0,10)At destination0Stop agentStopped
💡 Agent reached destination; remaining distance is 0, so agent stops.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
Agent Position(0,0,0)(1,0,1)(3,0,3)(6,0,6)(8,0,8)(9.5,0,9.5)(10,0,10)(10,0,10)
Remaining Distance14.1412.739.905.652.830.7100
Agent StateIdleMovingMovingMovingMovingMovingStoppedStopped
Key Moments - 3 Insights
Why does the agent keep moving even when the remaining distance is small but not zero?
Because the agent stops only when the remaining distance is less than or equal to the stopping distance (see step 6 in execution_table), not necessarily zero.
What happens if the path is not calculated yet when checking remaining distance?
The agent waits (pathPending is true) and does not move until the path is ready, ensuring smooth movement (refer to code in execution_sample).
Why does the agent stop exactly at the destination position?
Because when remainingDistance reaches zero, the agent sets isStopped to true, halting movement (see step 7 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the agent's position at step 4?
A(8,0,8)
B(3,0,3)
C(6,0,6)
D(9.5,0,9.5)
💡 Hint
Check the 'Agent Position' column at step 4 in the execution_table.
At which step does the agent stop moving?
AStep 7
BStep 6
CStep 5
DStep 3
💡 Hint
Look at the 'Agent State' column in execution_table to find when it changes to 'Stopped'.
If the stoppingDistance was increased, how would the agent's stopping step change?
AAgent would stop later, closer to destination
BAgent would stop earlier, at a larger remaining distance
CAgent would never stop
DAgent's path would change
💡 Hint
Refer to the condition in the code where agent stops when remainingDistance <= stoppingDistance.
Concept Snapshot
NavMesh Agent moves on a NavMesh towards a set destination.
Use SetDestination() to tell agent where to go.
Agent calculates path and moves step-by-step.
Agent stops when within stoppingDistance of target.
Check pathPending and remainingDistance to control movement.
Full Transcript
The NavMesh Agent component in Unity moves a game object along a path calculated on a NavMesh towards a destination. First, you set the destination using SetDestination(). The agent calculates the path and starts moving. Each update, the agent's position changes closer to the target, and the remaining distance decreases. When the remaining distance is less than or equal to the stopping distance, the agent stops moving. The code checks if the path is ready before moving to avoid errors. This process repeats until the agent reaches the destination and stops.