- Home /
2d path finding in C#
I need help with getting a script that can find the shortest path between one point to another on a 2d plane... even if there are obsticals in the way. The obsticals will all have a collider on them. I am then going to have a GameObject travel along that path at "x" speed.
Any Ideas? I really am having trouble with this... I dont even know where to start looking.
Answer by Mortennobel · Aug 23, 2011 at 07:31 PM
You could use A* pathfinding.
I have written a blog post about the topic:
http://blog.nobel-joergensen.com/2011/02/26/a-path-finding-algorithm-in-unity/
But my implementation is very general, which means that you would need to do some coding to get it to work with your needs.
Another option is to use one of the more movement specific implementations:
Answer by Rennat · Aug 23, 2011 at 07:35 PM
If you want any type of intelligent path finding you'll need to look into a pathfinding algorithm. The one I've heard of the most in video games is the A* (a-star) method.
The YouTuber @Dooobs has some videos showing off his A* pathfinding algorithms with demos that really helped me understand it.
Answer by Gurc · Mar 22, 2013 at 12:17 PM
Try this:http://www.arongranberg.com/astar/docs/ Note: I must say I have not tried other pathfindings, so there may be something better out there. So consider that I talk as a newbie. Opinion: Wonderfull Pathfinding. I tried it after seeing here. In the last Unity version there is compile error because of the default function values. I had to clean default values, noted them on paper(definitely you should do so) and corrected the function calls. After that it works like a charm.
This pathfinding can be used in Editor, you can define which layers to avoid, which layers are walkable. Probably there are a lot more but I just started. A definitely must have. Saves you months and painful debugging.