Pathfinding in games: A* and navigation meshes explained
Whenever an enemy chases you around obstacles or a unit in a strategy game finds a route across a map, pathfinding is at work. Here are the core ideas.
Representing the world
Before finding a path, the game needs a simplified map of walkable space:
- Grids: the world is split into tiles. Simple and common in 2D, tactics and strategy games.
- Waypoint graphs: hand-placed points connected by links. Lightweight but can look robotic.
- Navigation meshes (navmeshes): polygons covering walkable surfaces. The standard for most 3D games; Unity, Unreal and Godot all have built-in navmesh tools.
The A* algorithm
A* ("A-star") is the most widely used pathfinding algorithm. It explores possible routes, prioritizing those that look promising based on:
- g: the cost of the path so far
- h: a heuristic estimate of the remaining distance to the goal
It picks the node with the lowest g + h next. With a sensible heuristic (like straight-line distance), A* finds the shortest path efficiently.
Practical concerns
- Path smoothing: raw paths on grids are jagged; smoothing makes movement look natural.
- Dynamic obstacles: moving objects require local avoidance or re-pathing.
- Many agents: large crowds often use flow fields instead of individual A* searches.
- Performance: spread pathfinding across frames or run it on a background thread if many agents need paths.
Making AI feel smart
Players often perceive "good AI" from movement quality as much as decision-making. Good pathing plus small touches like pausing or looking around goes a long way.
What pathfinding approach do you use? Have you tried flow fields for large numbers of units?
Comments · 3
The explanation of the heuristic finally made A* click. It's Dijkstra with a hunch about which direction the goal is in.
Navmeshes are great for 3D worlds because you search over polygons instead of a grid of tiny cells. Much cheaper for open levels.
Please make sure the enemies don't all take the same path and bunch up. Nothing breaks immersion like a conga line of guards.