A* Pathfinding

The focus for the A* Pathfinding project is to provide an out-of-the-box pathfinding system.
The system is easy to set up and it scans the surroundings automatically.

  • Terrain grid
  • Terrain Demo

    Building Demo

    Navmesh Demo

    Features

    Easy to set up
    Requires no external data or code libraries
    Very fast (see performance)
    Can use multiple grids
    3D pathfinding is supported
    Runtime changing of the grid, for example, placing a building on the ground
    Node linking
    Free for non-commercial projects (see license for info about commercial use)
    Can execute over several frames (meaning less/no lag)
    Path caching
    Path simplification
    Support for navmeshes
    Support for creating your own procedural grid generators
    Works with Unity iPhone

    Performance

    For example: A path with 168 points and which searched 316 nodes took about 0.1 seconds to complete, it was spread over 10 frames.
    Another more complicated example: A path with 156 nodes and which searched 3730 nodes took about 0.43 seconds to complete and was spread over 42 frames. These example show much longer paths than you are likely to need (in case you are not making an RTS game).
    An example which is a bit more normal goes like this: A path with 33 nodes took about 0.006 seconds to complete, it was spread over 1 frame and searched 162 nodes.

    The 2.8 beta version which has just been released is quite a lot faster though.
    For example, a path with 137 nodes and which searched 3022 nodes took 0.0036 seconds to complete.
    Compared to the example with 156 nodes on the previous system this is about 100 times faster, however that test was done on a slower computer, when I have done controlled tests on the same computer the beta was about 9-10 times faster compared to the old system (2.52).

    Results can very very much depending on computer specs and things like that, so it is hard to give a good example.
    These tests were done in the Unity editor.

    Licence

    Creative Commons License
    A* Pathfinding by Aron Granberg is licensed under a Creative Commons Attribution-Noncommercial 3.0 Unported License.
    If you want to use the code for commercial projects please contact me at aron.g@me.com.
    A license to use the project/code in commercial applications/games costs US $100, although other options such as profit sharing can be discussed if that suits the person/company in question better.

    Download

    Download A* Pathfinding Project version 2.5 (or version 2.8 beta)

    Documentation

    All Docs
    Get Started Guide?
    FAQ
    How do I use my code with the A* script?
    API and components
    Using Unity iPhone

    How does A* pathfinding work?

    If you have any questions or things you would want to see added to the system, please post it as a comment or write to me at aron[dot] g[at]me[dot]com.

    64 Responses to A* Pathfinding

    1. Philip says:

      Real nice job. This is exactly what the unity community needs. Have you had a look at the community RTS project? Maybe you’d like to implement your system there.

    2. Gaba says:

      Wow, really nice job!

      Works almost perfectly, the only thing is that i sometimes get a null reference exception here

      ath..ctor (UnityEngine.Vector3,UnityEngine.Vector3,single,single,bool) (at Assets\Scripts\Pathfinding\AstarPath.cs:258)
      Seeker.StartPath (UnityEngine.Vector3,UnityEngine.Vector3) (at Assets\Scripts\Pathfinding\Seeker.cs:53)

      the code works perfect for controlled units, but if i estabilish waypoints form them to follow (i get the path from one waypoint to another), it gives me this exception, apparently its about the maxFrameCount

      if you could help on this i would appreciate a lot

    3. admin says:

      @Gaba
      The only scenario I can think of when this would happen is when you try to find a path in the Start or Awake function, since the variable “active”, also used in that line, is first set in Start, supposing you haven’t changed anything in the code.
      Hope that will help : )

    4. Pingback: Progress « jjmcgp

    5. Pingback: Movement upgrades, and firing! « jjmcgp

    6. woodgamesfx says:

      How can be A* used for commercial purposes ?

    7. admin says:

      If you mean: “what commercial application types can it be used in?”:
      A* pathfinding is used very often in RTS games and sometimes in TD games too, a lot of them are commercial.

      If you mean: “What shall I do to be able to use the A* project for commercial purposes?”:
      Send a mail to me [aron.g@me.com].

    8. Pingback: Sortie du A* Pathfinding 2.2

    9. kolk3 says:

      Hi, Im new in Unity3d. How I setup the A* pathfinding? thkz!

    10. admin says:

      You can download the example project to see an example of the A* system in action.
      The documentation page has a page about how to use your own scripts with the system.
      To set up the system in a new scene, it’s simply just to create a new gameObject and add the AstarPath script to that object.

    11. Zanmato says:

      Excellent addition mate! This will relieve many a headache when it comes to achieving those game development dreams. Keep up the good work! :D

    12. Michael says:

      Nice Job! This is exactly what i needed. Implementation worked very well, thank you!

    13. Pingback: Keeping the Path » Carrot Heist!

    14. Josh says:

      Hi i was wondering if the Astar Path code worked on any type of terrain because I have tryed a few time but just cant get it to work i keep getting the same error
      No nodes are walkable (maybe you should change the layer mask)
      UnityEngine.Debug:LogError(Object)
      AstarPath:Scan(Boolean, Int32) (at Assets\Garden AI\AstarPath.cs:1243)
      AstarPath:Start() (at Assets\Garden AI\AstarPath.cs:238)

    15. admin says:

      Yes it does (maybe I should write Get Started page…)
      The reason you are getting that error is probably because the script thinks the Terrain is an obstacle and thus making the nodes unwalkable.
      To make it work, place the terrain in a certain layer, “Ground” for example, then change the layer mask in the Walkable check settings to exclude the “Ground” layer, then the script won’t think the ground is an obstacle.
      Take a look at the terrain scene in the example project for a fully working setup.

    16. Josh says:

      Thnx That will help alot

    17. Someone says:

      Hi,
      I’ve downloaded A* Pathfinding and am quite confused. Is there any tutorial how to use this system at all? How, for example install it in Unity? How to generate navmesh? etc. The only thing I found in documentation folder is four png images, is it supposed to be a documentation?

    18. admin says:

      That’s what I am writing right now, documentation.

      For the release of version 2.5 I want to have a good “Get Started” guide, so hold on, the docs are not far away ; )

      Meanwhile, if you have any specific questions you can post them as a comment in the FAQ page.

    19. Default says:

      Then I am looking forward detailed documentation to be relesed. I wish you all the best in your job.
      As for specific questions, could you please tell how to install pathfinding in Unity? I can not find any pathfinding specific dialogs (shown in the screenshots) in Unity. It seems some plugin should be installed or I am wrong? On the other hand, the archive I’ve downloaded contains only assets and library folder (besides documentation) should I copy them to my project directory? I would like to keep project directory as “clean” as possible – no additional fbx, material, blender files etc. needed – is there anywhere a basic package that only contains necessary items, without additional models and such things?

    20. admin says:

      The folder you have downloaded is a complete unity project, to open up the project double click on the unity level file in Assets–>Terrain (maybe Assets–>_Scenes–>Terrain).
      You should have a look at these example scenes before you add it to your own project.
      To download only the necessary files, go to the download page and download the Unity package for upgraders, open your project in Unity and double click on the .unitypackage file to import the files. To add the A* script to a project, drag the AstarPath script to an empty gameObject and you should see the full editor controls there.

      WARNING: Don’t copy the library folder to another project, I did that once an Unity got a “Critical Error” message.

    21. Willem says:

      I was wondering what the status on the “Getting Started” guide is? I really want to use this for a project but I am a noob and need some help getting started :(

    22. admin says:

      @Willem

      Here’s the link for the current version of the “Getting Started” guide, the only problem is that the guide is written for 2.5 and not 2.2 which you are using, but you can probably get something out of it anyway :)

      http://www.arongranberg.com/unity/a-pathfinding/docs/get-started/

    23. brian says:

      Hi Aron, I was just wondering if you could tell me if it’s possible, and if so how, to manually add in the nodes that make up the NavMesh instead of using prebuilt tools. The reason I ask is because I have a game that generates levels from tilemaps, they’re all flat and it’s essentially the exact same approach as used by the image example. I mean I could go through the gigantic hassle of encoding the info into a colour coded image then passing that through but obviously that’s rather absurd. Anyhoo thanks in advance for any help you can give on this area, i’ve implemented A* myself for this project but it’s not very well optimised and this is an iphone project so it’s somewhat important.

    24. Admin says:

      Hi Brian

      I created a new grid generator based on the texture generator (5 minutes of work) just for you!
      The only thing that changed is that it takes an array of booleans (true/false) to generate the grid along with width and depth variables.
      Take a look at the ScanTilemap function when the system gets released (in the code).
      Make sure you use the Texture mode and that the Calculate On Startup flag is set to FALSE, you will need to call the function yourself to generate the grid since it needs some extra data (bool array).

      PS: Want to beta test the new system? The release is very near but I need some to test the system since I don’t think that I can debug the system all by myself.

    25. brian says:

      Brilliant! Thanks alot, ofcourse i’d be happy to test it, having a reliable, latent processing pathfinder will be of immeasurable help so i’m glad to help in any way I can. You can contact me at ******* (removed)!

    26. Robin says:

      Hi, I am working on intelligents agents and want each agent to calculate an optimal path in a terrain that is unique to each agent. Consider ten robots positioned in the same physical terrain. Each robot has its own target position and an internal representation of the terrain called an “abstract map.” That is, each robot views the landscape with their own eyes and sees the terrain differently. Therefore I am currently generating ten extra maps (ten unique terrains) and adding the AstarPath script to each map, with the idea that each robot plans an optimal path based on their own abstract map.

      Is it possible to use more than one AstarScript at the same time? Do you have any other suggestions for my idea?

    27. Admin says:

      Hi Robin

      The AstarPath script is a singleton class, so you can’t use more than one in each scene.
      What you can do (when 2.5 comes out) is to make multiple grids which raycasts and hits different layers, thus giving you 10 different maps.
      In 2.5, the seeker script will have a variable called Grid Selection, by default this is set to Auto, with that setting it chooses between all grids, but you can set it to Fixed and change another variable, called just “Grid” to the grid index you want it to search.
      For example you want one agent to operate on grid 3 (number four in the grid list, remember an array starts on 0), you set “Grid Selection” to Auto and “Grid” to 3, then that seeker will only search the fourth grid.

      You will only be able to follow these instructions when 2.5 is released, hopefully this week.

    28. Jordan says:

      Hi Aron, been using your system since back in september and its been working brilliantly so far, however ive just tried to update my RTS project to 2.5 that you just released. Im getting 2 errors which i have previously never encountered:

      Assets/Editor/AstarPathEditor.cs(353,43): error CS0117: `Selection’ does not contain a definition for `activeObject’

      Assets/Editor/AstarPathEditor.cs(354,57): error CS0117: `Selection’ does not contain a definition for `GetFiltered’

      Im not exactly sure what this relates too, all the new scripts are in the same folders they were in previously when they worked with the last version of the pathfinding. The Editor Default Resources folder is correctly placed such as the example project that you created, so im unsure exactly whats causing these errors.

      I dont suppose you know whats causing these errors ?

      Cheers
      Jordan

    29. Admin says:

      Hi Jordan

      Are you sure you are using the latest version of Unity (2.61)?
      Or are you using Unity iPhone? In Unity iPhone, editor scripts wont work, but if that was the case you would probably have gotten a bunch of other errors too.

    30. Jordan says:

      Hey again, just realised ive only got unity 2.6 ^^ updgrading it to 2.61 now , its not the i-phone version either. Ill post back if this doesnt fix it, but it probably will i just missed what version was required .

      Cheers
      Jordan

    31. Jordan says:

      Hi again, again. Upgrading unity to the most recent version doesnt seem to have fixed these errors sadly, still getting the same 2 as above.

      Jordan

    32. Admin says:

      Weird…

      Well, if you have got the latest Unity version, and you are not using Unity iPhone, then Selection should contain those definitions, I have no idea why it doesn’t work on your computer.
      Is it working in the example project when you haven’t changed anything (re-download it if you have changed something)?

      Maybe it is a Unity bug.

      Aron

    33. Jordan says:

      it works fine within the example project, thats one of the first things i checked when it didnt work. I have tried reimporting all the scripts again and that hasnt work either. The folder structure is identical to the example project, bar the AstarPath/math/classes and seeker files, which are in the standard assets folder so i can access them with javascript, however i still receive the same errors if they were in the Pathfinding folder.
      Aditionaly i havent changed anything within the scripts themselfs.

      Cheers
      Jordan

    34. Admin says:

      Try to delete all the pathfinding scripts and folders, and then importing a package named allRequiredAssets2_5.unitypackage which should exist in the example project folder (outside the assets folder).

      Aron

    35. Jordan says:

      Hey, seems ive found the solution now, i forgot i allready had a file named Selection deep inside some load of subfolders in my project, I think this would make a difference, ive since renamed it and the errors disapear. Just threw me off scent as they point to lines within the Astar script itself. Seems that you cant name a file Selection, but o well :) fixed now.
      Cheers for your help
      Jordan

    36. Admin says:

      Ah, great that you solved it! :D

      No, I would not recommend naming a file Selection, one of Unity’s editor classes is also named Selection.

    37. Pingback: Cygnus X1B.log » A* Pathfinding para Unity

    38. iPhoneDeveloper says:

      Hi Aaron,
      I wonder if you have plans to release an Unity iPhone version?
      Theres a lots of Unity iPhone users willing to pay for this, thus us too.
      You could automatically put the iPhone version in a commercial state and sold it entirely, no need to be free.

      Admin: AFAIK the A* Project works with Unity iPhone, I haven’t tested it myself but others have said it works perfectly, although the custom editor does not work since Unity iPhone doesn’t use the new UI which makes stuff a bit more complicated, but does still work.

    39. Kevin says:

      I have just started using the A* Pathfinding system. I’m very impressed. I do have one question, does it support multiple adjacent grids in a single scene? For example, could I have a mesh grid and a list grid in the same scene and find a path across the grid boundaries?

      Thanks

    40. Gonomonon says:

      Hey Aron,
      Awesome program, I love it – I’m not very good with scripting, so I apologize if this is really easy, but I’m wondering what the procedure would be to allow the character to move towards the clicked point even if the player clicks on a non-walkable area or in another zone.
      For example: if I have two zones with no connections between them (say, a wall is in the way), and I click on the zone that the character is NOT in, would it be possible for the character to walk as far as he could towards the click, and stop when he can’t go any further?
      So the character will walk somewhere, no matter where you click, even if he can’t get there.

      Thanks a lot

    41. Admin says:

      @Gonomonon

      Sorry, currently that is not possible, it is on my ToDo list for the next version though.

    42. Admin says:

      @Kevin

      Sorry, this is not possible at the moment, I have it on my ToDo list for the next version though.

      However, you could code it up yourself using a procedural grid, but that might be a bit hard.

    43. Gonomonon says:

      Aron,
      Thanks, I’ll have to wait then.

      If I could suggest something that i think would allow that to work, and also be useful for other things (in my opinion)
      It would be nice if the user could assign an individual walkable node (or group of nodes, or hand placed links) to any given target that you click on.
      For instance, you could set up a invisible geometry cube or trigger, and then tell A* to assign a path node that the character would walk to if the player clicked on that cube.
      It seems like that might allow the user to kind of “direct” where the character goes in a given situation.

      But, as I said, I’m no programmer – maybe I’m way off ^_^

      Thanks again

    44. Nic says:

      Hi Aron,

      Been using the tool for a few weeks now in our new project and it’s all kinds of awesome. However, I got a problem that comes up sometimes. Sometimes when I run the game the grid calculated at startup screws up and makes the whole walkable terrain un-walkable. I usually have to save the scene, load another then reload and it’s okay again. If I turn off calc grid on startup then it errors with this:

      NullReferenceException: Object reference not set to an instance of an object

      Happy to send you a copy of the project if you want to take a look. It’s probably something I’m doing wrong.

      Cheers!

      Nic.

    45. Admin says:

      Hi Nic

      Hmm, sounds weird, I haven’t seen that before.
      I would be very glad if you could send the project to me (the email can be found on the A* page (don’t want to post it in to many places)).

      Cheers!
      Aron

    46. Rob says:

      Hi Aron, this is really fantastic!

      Something I’ve noticed in my attempts to use it is that the Mesh format seems to be a little quirky. I think it’s related to the right-handed/left-handed coordinate systems of Maya, Unity, etc.

      For me, I make an environment in Maya, and export it into Unity. But when I generate the navmesh using A*, it is inverted! I cannot rotate it to match the mesh because the the handed-ness of the system seems to be opposite.

      Perhaps in an upcoming version you could add an option to make one of the axes negative on the navmesh so that it could align with an imported mesh of a differing handedness?

      Comment 2: … a follow-up, it seems that setting the scale to -1 works! Thanks for that. Now I just have to figure out how to make it so the mesh isn’t entirely walkable… seems there is no concept of slopes/raycasting on meshes? Maybe I’m wrong about that too.

      I’ll look into it a bit longer before hammering you with comments! :)

      For now I will try exporting to FBX and see if that can solve the problem. but I would really like to use Unity’s built-in .mb and .ma importers!

    47. Admin says:

      Hi Rob

      1. I have also noticed the problems with aligning the navmesh, I will sure add the functionality of inverting the axis in the next version of the system.

      2. Well, a navmesh is used to specify where the player can walk, this is not the same as the rendered mesh since the rendered mesh can include walls and other stuff which you don’t want the player to be able to walk on (in most cases anyway), I am working on adding slopes/angles to the mesh mode, but so far you cannot use any angle dependant features with the mesh mode.

    48. Rob says:

      Hi Aron,
      if I were to release a project as a resource meaning under gpl or something would I be able to include your A* Pathfinding code in the release or would that be violating the terms of use ?

    49. Admin says:

      Hi Rob

      Sorry for not responding : (

      Hmm, good question… I think so, since you are redistributing it under a different license, maybe if you clearly state that the A* Pathfinding is from this page and that it is distributed under a different license.

      Regards,
      Aron

    50. Rick says:

      I’m trying to use your A* library in a 64×64 terrain but not having any luck. So I create an empty game object and named it A*. I set the Terrain object to the Ignore Raycast. When I do Scan Map I just get red squares and not the screen mesh like in your example. Any advice on how to get this working for terrains?

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>