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.

    78 Responses to A* Pathfinding

    1. Admin says:

      Hi

      I have explained the solution for that on the FAQ page
      http://www.arongranberg.com/unity/a-pathfinding/docs/faq/

      And also on the fourth latest post in the release thread.
      http://forum.unity3d.com/viewtopic.php?p=307688#307688

    2. Jimboner says:

      learn to write better instructions faggot.

    3. Admin says:

      If you have any suggestions of what I have missed, please post that instead of just complaining.

    4. Ricky Helgesson says:

      Incredible work! I am proud to be Swedish. :)

      I will try to use this to find paths for vehicles in an offroad vehicle game. Will send you the results and contact you for commercial use.

      /Ricky

    5. Pingback: I still have to come up with a good title.. » Pathfinder

    6. Felipi says:

      left this in the home page, but got no feedback so i’m posting it here as well^^.

      sorry to post for this… but just wanted to say HOW AMUZED I AM WITH THIS!!! congratulations, it’s an amazing project. had never worked with AI or pathfind, and the actual game i’m working needs it and i could set everything up in just 2 hours thanks to your project! Totally noob friendly!

      When i grow up, and start selling games i will buy it!

      AH! btw, i’m working in a Tower defense game! And, right now my enemies move in a straight line, but soon i intend to upgrade my tower defense to let players make a maze with their towers, and so, i will need to scan the map everytime a player set up a tower in my grid. Is there any way to scan the map again every time i place a tower? Or the only options to scan the map is on startup or manually? Is there any way to call the function “scanmap” from another script?

      Sorry if it’s somewhere on this site, but i didn’t find it anywhere!

      Thanks!

    7. Admin says:

      Hi Felipi

      Sorry, I’m a bit lazy with answering these comments.

      I have posted an answer for you now though (where you first posted).

      -Aron

    8. Awesome system! It’s fast and working really well. One note is that the documentation for me was a little confusing. Perhaps a video tutorial that demonstrates all the aspects of it would be best for these types (frameworks) of things. Thank you for offering it for free to the community! That was really generous of you; I think this should be built into Unity. When I get a little better I try to post a video tutorial on how to use your A-Star system or as I’ve been calling it, “Aron-Star” (Maybe you should brand that!).

    9. Mani says:

      I’m working on a pathfinding/ Artificial Intelligence resources. Will make sure to add this.

      M

    10. Pingback: [Unity3D] A* Pathfinding project - Uititled

    11. Adam Waters says:

      Hey Aron,
      I’m trying to bring your path finding scripts into my project, but I can’t get past this error:
      The type or namespace name `AstarData’ could not be found. Are you missing a using directive or an assembly reference? (CS0246) (Warlord)

    12. Richard says:

      Hey Aron,

      Great system, integrated it very easily into our application.
      We have run into a snag though, we need to move the A* grid procedurally. The relative positions of the nodes do not change, but the entire scene translates and rotates in the world space.
      Is there anything that would allow us to do this in the code already? If not, where would you recommend we start?

      Thanks!

    13. Admin says:

      There is no built in functions to do that unfortunately, you can however change the positions of the nodes by script.
      There is an array of twoD arrays in the AstarPath script called staticNodes it holds the instances of every node for every grid ( staticNodes[gridIndex][xIndex,zIndex] )
      You can iterate it and modify the positions of the node.
      staticNodes[0][12,43].vectorPos = new Vector3 (1,2,3);
      The formula for the default position of the nodes is (C#):
      node.vectorPos = new Vector3 ( ( (float)x+0.5F) * grid.nodeSize + grid.offset.x , grid.offset.y , ((float)z+0.5F) * grid.nodeSize + grid.offset.z );

      The only problem is when you rotate the grid you cannot use the fast function for determining the closest node to the player and you will have to use the very slow function (it is used for non-gridlike navmeshes).
      You will have to change some code to do that:
      In all ToLocal functions in the AstarPath script there should be some code like this:
      if (active.gridGenerator == GridGenerator.Bounds || active.gridGenerator == GridGenerator.List || active.gridGenerator == GridGenerator.Procedural) {
      return ToLocalTest (pos,forceNodesUnder);
      }

      Comment out the if in those functions to make it look like this:
      //if (active.gridGenerator == GridGenerator.Bounds || active.gridGenerator == GridGenerator.List || active.gridGenerator == GridGenerator.Procedural) {
      return ToLocalTest (pos,forceNodesUnder);
      //}

      This will force it to use the slow nearest point function.
      The first one is on line 599.

      Hope it helps,
      Aron

      Sorry for answering late, I have been (and am) on holiday, but I’m home for one day so I will try to answer as many comments as possible.

    14. Admin says:

      Hi Adam

      I have been (and am) on vacation so I haven’t been able to answer your comment until now.
      You can fix the problem by including the AstarData.cs script from the GridData folder in the example project (2.8beta) (I should have put it in the /pathfinding folder, but well, that’s why you have betas) and the AstarDataEditor.cs script from the Editor folder.

      The AstarData.asset asset isn’t needed for the project to work.

    15. John Keyworth says:

      Firstly I’d like to say this stuff is great. It pugged into my project really easily and seems to get the job done.

      I do have two questions though. I’m generating my terrain procedurally at run time, so I keep needing to add sections into the world. I’ve created each section of terrain as it’s own grid.

      1) Is there a function to re-scan just a single grid? Recalculating everything is quite expensive any time I add a new section. I know I can split it over several frames but I’d rather just do 1 bit at a time (I’ll probably split it over several frames anyway in the end).

      2) Each of the grids is a separate area and I can’t plot routes between them. How would I get around that?

      Thanks,
      John,

    16. Richard says:

      Hi Aron,

      We’ve run into a bit of a bug with your A*. The program starts off fine, all of the agents seek and behave normally. Then after a short time, they all break simultaneously. It does not appear to be caused by any particular event in the application, but here are some of the errors:
      - Agents we have told to stop will begin along their paths again.
      - Agents which would accurately detect distances begin to target miss their marks.

      Any idea what might be causing something like this? Again, it does not feel like any individual object is breaking, but rather the whole simulation all at once.

      Thanks for all your help!
      -Richard

    17. Richard says:

      Disregard the comment above Aron, we’ve discovered the problem almost immediately after posting. And it was in our own application.

      Sorry about that.

    18. Admin says:

      Hi John

      A bit tricky.
      You could fake it by calling myAstar.SetNodes (false, theGridBounds, true);
      Or modify the Scan () function to accept an int telling it which grid to calculate.

      2. Even trickier. The only solution I can come up with now is to manually loop through the edge nodes of each grid and add links to the other grids.
      You can get each grids node array from myAstar.staticNodes[theGridIndex] the latest added grid is the last in the array.

      You can add a connection to a node by calling myNode.AddConnection (new Connection (endNode : Node, cost : int, angle : float, enabled : bool));
      The cost is between nodes in a grid is 10 or 14 for diagonals.
      Mail me if you need more help.

    19. Erre Run says:

      Hi Aron,

      We integrated your A* Pathfinding about a month ago and it is working great so far.
      Please keep up the good work.

    20. Atty says:

      Hi Aron,

      You did a good job here, but I still have one question: do you have this a* path finding also for fps and 3ps?

      Thanks

    21. Admin says:

      Hi Atty

      As I replied to your other comment, this system works just as good for fps as for 3ps, there would be no difference in the system in a version for fps compared to one for 3ps.

    22. Atty says:

      But what if I want in my game the enemy to follow my player? how would I do that? I`m pointing to the help that you gave us with the files where to put and so on. Could you help me with a little explaination?

      Thanks

    23. Felipi Arena says:

      Hello! Me again!

      Everything is done and ready to be launched on our game. BUT… there is one little thing that is bothering me. It’s good the way it is, and it could easily stay like it is now.
      Well, The thing here is, i have enemies, and some tower add effects to them, like Stun and slow. They calculate the path PERFECTLY, but they can’t see the other enemies as obstacles. This means that, if front enemies gets stunned the enemies coming from behind will just crash behind them and get stuck until the front one is killed or goes back to normal speed.
      I tried some ways to implement it, messed things a bit but in the end didn’t work. I know it’s possible to be done with more hard work, but is there an easy way to do it? If not it’s ok! This project already helped us big time!

      Thanks!

    24. Admin says:

      Hi Felipi

      This is quite complicated to do with just pathfinding (in fact, I haven’t succeeded with it in a good way), it often requires a quite advanced AI for each unit (something which I haven’t succeeded with either).
      Usually TD games solve that by just not making the enemies collide with each other (like in TowerMadness, VectorTD for example).

    25. SNoChE says:

      I got this error every time I change code on visual studio and come back to unity to see the changes, looks like doesnt refresh properly and lost the navmesh. Any way to fix this is quiet annoying, thanks.

      ” navmesh is not calculated yet – Check the ‘Scan Map On Startup’ toggle to make sure the map is scanned at start – Don’t run any pathfinding calls in Awake
      UnityEngine.Debug:LogError(Object)
      Path:.ctor(Vector3, Vector3, Single, Single, Boolean) (at Assets\-Scripts\3th parties\Pathfinding\AstarPath.cs:919)
      Seeker:StartPath(Vector3, Vector3) (at Assets\-Scripts\3th parties\Pathfinding\Seeker.cs:258)
      c__CompilerGenerated9:MoveNext() (at Assets\-Scripts\Controlers\AIFollow.cs:78)”

    26. wall says:

      Great work!!
      Thank you very much for sharing your knowledge.
      It will be a great help for me.

    27. Admin says:

      @SNoChE

      Unfortunately there’s nothing I can do about that, for data to be persistent when you re-compile scripts the data must be serializable, and the grid data is just too large to be serialized by Unity (all my tries haven’t worked anyway).

    28. SNoChE says:

      Maybe this could help you a little if you haven’t read it b4

      http://answers.unity3d.com/questions/1559/edit-and-continue-scripting-destroys-current-state/1583#1583

      How about a variable to force rescan on errors or something like that for debugging purposes?
      Instead showing the message “The navmesh is not calculated yet – Check the ‘Scan Map On Startup’ ” will rescan all from the begining.

      Congratulations with your usefull script, I am Looking forward to Astarpath 3! and some object avoidance. ;)

    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>