Tower Defense Tutorial: Part 8 – AI Pathfinding
This tutorial will focus almost exclusively on setting up a fully-functional AI Pathfinding solution for our Tower Defense game! This paves the way for our ground-based attackers, allowing them to dynamically maneuver around turrets, even as you place them. Of course, AI Pathfinding can be useful for many other projects, so give this one your full attention!
What is covered in this tutorial:
- Downloading and importing the “A* Pathfinding Project”
- Setting up a grid-based A* Graph
- Creating a script to intelligently Path your Units
- Using the Character Controller component to move units along the AI Path
This tutorial uses the “A” Pathfinding Project



Thanks for the update! Do you know if this A* Pathfinding will work in Y space as well? For instance, imagine I have a large lake with things swimming around, could I have this Pathfinding work to go towards the Player who is also swimming around? OR, if I have a Balloon in the air that all the AI is going to move towards, but they have to go up stairs, or fly up.
Simply put, yes, it does.
Thanks You!(Myanmar)
guys i’ve got a problem. i followed the tutorial, and made the script the very same way. but when i save it the console says
Assets/1Scripts/Pathfinding.js(1,1): BCE0055: Internal compiler error: Boo.Lang.Compiler.TypeSystem.Ambiguous.
i tried to change some things, but still it is angry towards my
import Pathfinding;
on the unity forum it was proposed to add the #pragma strict line, but even when i did it the same error appeared… don’t really know where to hit my computer to make it work
It almost sounds like you double imported something…did you also make sure to swap your AI Pathfinding Project to JavaScript mode, like in the tutorial?
Woot, a new tutorial!
THis is awesome, keep it coming!
I had to make 2 changes probably due to a* pathfinding script updates.
path.vectorPath.Length should to be replaced by path.vectorPath.Count
Another point : if I keep “dir *= speed * Time.fixedDeltaTime;”, the capsule hits the wall and doesn’t reach the final point.
If I only put “dir *= speed;”, everything is fine except capsule speed which is very, very fast.
I don’t really know how to fix this issue.
Any thoughts?
This shouldn’t happen, multiplying by Time.DeltaTime means it will average per second rather than per frame so it keeps a consistent speed, my guess is something else is causing the problem
-Alex
He is right,
the capsule is very very very slow and had to remove the time factor,
now about the hitting the wall;
I went back to our _A* GameObject and changed “Collision -> Diameter” to >1.5 and hit scan the problem should not occur again
to anyone who has problem with the “Length not being a member of…”
just replace the bad code with this :
if (currentWaypoint >= (path.vectorPath as Vector3[]).Length)
Enjoy !
That will hurt performance *a lot*. You are making a copy of the path.vectorPath List every single fixed update which is not nice on performance or the GC. Consider instead to use path.vectorPath.Count instead of path.vectorPath.Length. Since the tutorial was made there have been a few updates to the project, one of them changed the path.vectorPath variable from a Vector3 array (Vector3[]) to a Vector3 List (List<Vector3) this change requires that you use .Count instead of .Length.
Well,
that explains the low FPS haha !
What is your strategy for preventing the player from blocking the path to the target? For example building a bunch of towers around the target
If I move an object into my enemy’s path, the enemy will not navigate around it. How do you solve this?