Bird
Raised Fist0
Unityframework~20 mins

Pathfinding basics in Unity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Pathfinding Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Unity C# code snippet?

Consider this code that attempts to find a path using a simple grid and prints the path length.

Unity
using UnityEngine;
using System.Collections.Generic;

public class PathfindingTest : MonoBehaviour {
    void Start() {
        List<Vector2Int> path = new List<Vector2Int> {
            new Vector2Int(0,0),
            new Vector2Int(1,0),
            new Vector2Int(1,1),
            new Vector2Int(2,1)
        };
        Debug.Log("Path length: " + path.Count);
    }
}
APath length: 3
BPath length: 4
CPath length: 5
DPath length: 0
Attempts:
2 left
💡 Hint

Count how many points are in the path list.

🧠 Conceptual
intermediate
1:30remaining
Which data structure is best for storing nodes to explore in a breadth-first search (BFS)?

In pathfinding, BFS explores nodes level by level. Which data structure fits this behavior best?

ADictionary
BStack (LIFO)
CHashSet
DQueue (FIFO)
Attempts:
2 left
💡 Hint

BFS explores nodes in the order they are discovered, first in, first out.

🔧 Debug
advanced
2:00remaining
What error does this Unity C# pathfinding code produce?

Examine this code snippet that tries to access a dictionary of nodes by key.

Unity
using System.Collections.Generic;
using UnityEngine;

public class PathfindingDebug : MonoBehaviour {
    void Start() {
        Dictionary<Vector2Int, int> nodeCosts = new Dictionary<Vector2Int, int>();
        nodeCosts[new Vector2Int(0,0)] = 1;
        int cost = nodeCosts[new Vector2Int(1,1)];
        Debug.Log(cost);
    }
}
AKeyNotFoundException
BNullReferenceException
CIndexOutOfRangeException
DNo error, outputs 0
Attempts:
2 left
💡 Hint

Check if the key exists before accessing the dictionary.

📝 Syntax
advanced
1:30remaining
Which option correctly declares a method to calculate Manhattan distance between two Vector2Int points?

Manhattan distance is the sum of absolute differences of x and y coordinates.

Aint ManhattanDistance(Vector2Int a, Vector2Int b) => Mathf.Abs(a.x - b.x) + Mathf.Abs(a.y - b.y);
Bint ManhattanDistance(Vector2Int a, Vector2Int b) { return abs(a.x - b.x) + abs(a.y - b.y); }
Cint ManhattanDistance(Vector2Int a, Vector2Int b) { return Mathf.Abs(a.x - b.x) + Mathf.Abs(a.y - b.y); }
Dint ManhattanDistance(Vector2Int a, Vector2Int b) { return Mathf.abs(a.x - b.x) + Mathf.Abs(a.y - b.y); }
Attempts:
2 left
💡 Hint

Check correct method syntax and correct function names in Unity.

🚀 Application
expert
3:00remaining
What is the number of nodes visited by this A* pathfinding pseudocode on a 3x3 grid with no obstacles?

Given a 3x3 grid from (0,0) to (2,2), start at (0,0), goal at (2,2), and A* using Manhattan distance heuristic, how many nodes are visited?

Assume neighbors are up, down, left, right, no diagonals.

Unity
Start = (0,0)
Goal = (2,2)
Grid size = 3x3
Heuristic = Manhattan distance
Neighbors = 4 directions
No obstacles
A7
B6
C9
D5
Attempts:
2 left
💡 Hint

Trace the A* search expanding nodes with lowest f = g + h.

Practice

(1/5)
1. What is the main purpose of pathfinding in Unity games?
easy
A. To help game objects move from one point to another avoiding obstacles
B. To create visual effects like explosions
C. To manage game audio settings
D. To design user interface layouts

Solution

  1. Step 1: Understand pathfinding concept

    Pathfinding is used to find a route for characters or objects to move smartly in a game world.
  2. Step 2: Identify Unity's use of pathfinding

    Unity uses pathfinding to move game objects from one point to another while avoiding obstacles.
  3. Final Answer:

    To help game objects move from one point to another avoiding obstacles -> Option A
  4. Quick Check:

    Pathfinding = smart movement [OK]
Hint: Pathfinding = smart movement avoiding obstacles [OK]
Common Mistakes:
  • Confusing pathfinding with graphics or audio
  • Thinking pathfinding manages UI
  • Assuming pathfinding creates effects
2. Which of the following is the correct way to set a destination for a NavMeshAgent in Unity?
easy
A. agent.Navigate(target.position);
B. agent.MoveTo(target.position);
C. agent.GoTo(target.position);
D. agent.SetDestination(target.position);

Solution

  1. Step 1: Recall NavMeshAgent method for setting destination

    The correct method to set a destination is SetDestination(Vector3 target).
  2. Step 2: Match method names with options

    Only agent.SetDestination(target.position); uses SetDestination correctly; others are invalid method names.
  3. Final Answer:

    agent.SetDestination(target.position); -> Option D
  4. Quick Check:

    SetDestination sets target [OK]
Hint: Remember NavMeshAgent uses SetDestination() method [OK]
Common Mistakes:
  • Using non-existent methods like MoveTo or GoTo
  • Forgetting to pass target.position as Vector3
  • Confusing method names with other APIs
3. What will be the output of this Unity C# code snippet?
using UnityEngine;
using UnityEngine.AI;

public class MoveAgent : MonoBehaviour {
    public Transform target;
    private NavMeshAgent agent;

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

    void Update() {
        if (agent.remainingDistance < 0.5f) {
            Debug.Log("Reached target");
        }
    }
}
medium
A. Logs "Reached target" immediately on start
B. Logs "Reached target" when agent is close to target
C. Throws a runtime error because NavMeshAgent is missing
D. Never logs anything

Solution

  1. Step 1: Understand NavMeshAgent behavior in Start()

    The agent sets its destination to the target's position at start.
  2. Step 2: Check Update() condition for logging

    When the agent is within 0.5 units of the target, it logs "Reached target".
  3. Final Answer:

    Logs "Reached target" when agent is close to target -> Option B
  4. Quick Check:

    Agent logs on close arrival [OK]
Hint: remainingDistance < 0.5 triggers log near target [OK]
Common Mistakes:
  • Assuming log happens immediately
  • Ignoring NavMeshAgent setup requirement
  • Thinking it throws error without NavMeshAgent component
4. Identify the error in this Unity C# code for pathfinding:
public class AgentMover : MonoBehaviour {
    public Transform target;
    private NavMeshAgent agent;

    void Start() {
        agent = GetComponent<NavMeshAgent>();
        agent.SetDestination(target);
    }
}
medium
A. SetDestination requires a Vector3, not a Transform
B. NavMeshAgent component is not assigned
C. Missing Update method to move agent
D. target variable should be private

Solution

  1. Step 1: Check SetDestination parameter type

    SetDestination expects a Vector3 position, but target is a Transform object.
  2. Step 2: Correct usage of SetDestination

    Use target.position to pass Vector3 instead of target directly.
  3. Final Answer:

    SetDestination requires a Vector3, not a Transform -> Option A
  4. Quick Check:

    SetDestination needs Vector3 [OK]
Hint: Use target.position, not target, in SetDestination [OK]
Common Mistakes:
  • Passing Transform instead of Vector3
  • Assuming NavMeshAgent auto-assigns
  • Thinking Update method is mandatory for movement
5. You want a NavMeshAgent to move to a target only if the path is valid and reachable. Which code snippet correctly checks this before moving?
hard
A. if (agent.CalculatePath(target.position, new NavMeshPath()) && agent.SetDestination(target.position)) { /* move */ }
B. agent.SetDestination(target.position); if (agent.pathStatus == NavMeshPathStatus.PathComplete) { /* move */ }
C. NavMeshPath path = new NavMeshPath(); if (agent.CalculatePath(target.position, path) && path.status == NavMeshPathStatus.PathComplete) { agent.SetDestination(target.position); }
D. if (agent.isOnNavMesh) { agent.SetDestination(target.position); }

Solution

  1. Step 1: Understand path validation with CalculatePath

    CalculatePath fills a NavMeshPath object and returns true if path calculation succeeded.
  2. Step 2: Check path status for completeness

    Only if path.status is PathComplete is the path valid and reachable.
  3. Step 3: Set destination only if path is valid

    NavMeshPath path = new NavMeshPath(); if (agent.CalculatePath(target.position, path) && path.status == NavMeshPathStatus.PathComplete) { agent.SetDestination(target.position); } correctly creates a path, checks status, then sets destination.
  4. Final Answer:

    NavMeshPath path = new NavMeshPath(); if (agent.CalculatePath(target.position, path) && path.status == NavMeshPathStatus.PathComplete) { agent.SetDestination(target.position); } -> Option C
  5. Quick Check:

    CalculatePath + PathComplete check = safe move [OK]
Hint: Use CalculatePath and check PathComplete before SetDestination [OK]
Common Mistakes:
  • Ignoring path status after CalculatePath
  • Calling SetDestination without path check
  • Using wrong method signatures
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard