A* pathfinding
How enemies chase you around walls instead of into them.
Moving straight at the player works until the first wall. A* explores outward from the start, always trying the step that looks cheapest so far plus a guess of the distance left, and finds the shortest route without searching the whole map. The guess is what makes it much faster than a blind search.
Try this: Run around the walls: the red slime uses A* and follows you round corners. Turn on the straight-line chaser (C): the grey one walks into the first wall and gets stuck. Click tiles to build walls, or wall yourself in to see the slime give up.
exploredA* pathwall
Run! The slime re-plans its route every 0.25 s.
Prompt bit
Works for 2D or 3D in any engine (Unity, Unreal, Godot, web). Paste the short one for a quick fix, or the detailed one when you want it done properly.
Add A* pathfinding on a walkable grid: expand the open tile with the lowest cost-so-far plus Manhattan estimate, recompute a path at most every 0.5 s rather than every frame, and return the closest reachable tile when the goal is blocked instead of freezing.
Implement A* pathfinding for enemies and click-to-move (2D grids or 3D, any engine). Build a walkability grid from the level (or use the engine's navmesh in 3D: Unity NavMesh, Godot NavigationServer, Unreal NavMesh, which already run A*). For each search keep an open set as a priority queue ordered by f = g + h, where g is the cost so far and h is the estimate: Manhattan for 4-way moves, octile for 8-way; never overestimate, or paths stop being shortest. Track cameFrom per tile and rebuild the path when the goal is popped. With diagonals, block corner cutting past walls. Use terrain costs (mud 3, road 1) in g. Limit each search to a node budget (default 2000) and, if the goal is unreachable or the budget is hit, return the path to the closest tile found. Recompute paths at most every 0.5 s or when the goal moves more than a tile, and stagger enemies across frames. Smooth the result with line-of-sight checks so units do not zig-zag. Draw the path in debug.
Where it's used
- RTS and tactics games: unit movement and click-to-move
- Top-down and roguelikes: enemies hunting the player around walls
- 3D games: navmesh agents use A* under the hood
- Tower defence: creeps re-pathing when you build
- Puzzle games: hints and solvability checks
Common mistakes
Re-pathing every frame for every enemy, an estimate that overestimates (paths stop being shortest), searching forever when the goal is walled off, and letting diagonal moves cut through wall corners.
Writing this with an AI assistant? See our best AI coding assistants for game developers.
Missing a concept? Request one.



