Search Unity

AI: Pathfinding in Unity: Path

Discussion in 'Assets and Asset Store' started by AngryAnt, Feb 15, 2011.

  1. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Update 2
    Hey guys. Obviously I've been pretty busy and since I'd like to spend the spare time coding time I have on Behave, I have just made the Path repository publicly available with an MIT license attached.

    https://github.com/AngryAnt/Path

    Note that I will be monitoring and taking in pull requests as well as compiling builds for releasing into the asset store (obviously as a free package). Let's make this project as awesome as can be :)

    Issue tracking and wiki development (I would appreciate collaborators on that as well) is still happening on https://github.com/AngryAnt/Path-release.

    Happy hacking!

    Original blog post:

    http://angryant.com/2012/05/17/path-is-now-mit-licensed/


    Update 1

    Added a brief tutorial to the Path documentation:

    "Pathfinding in two lines": http://angryant.com/2011/02/15/pathfinding-in-two-line/


    Original post

    That's right! Path is back with version 2: easier to use, more powerful than before and finally available for Unity 3.x.


    Pathfinding in Unity has never been easier.
    • Set up waypoints in your scene using standard transform tools.
    • Auto-scale and -connect with the click of a button.
    • Start a path search via a single variable assigning.
    • Receive pathfinding results as messages on the GameObject of origin.

    Get as advanced as you need to.
    • Parallel pathfinding requests.
    • Custom weights based on tagging.
    • Path invalidation call-back when already found pathes are rendered invalid.
    • Throttle quality vs. performance at runtime.

    As per usual, Path is free to use with a show-logo clause. More details in the license.


    You'll find documentation available here:

    http://angryant.com/path/documentation/


    And a link to the asset store page plus an example seeker script here:

    http://angryant.com/path/download/


    Original blog post:

    http://angryant.com/2011/02/15/path-2-released/


    Have fun!
     
    Last edited: Aug 30, 2012
  2. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Last edited: Oct 13, 2012
  3. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    Looks nice and simple to use Emil... Great work. Cant wait to give it a whirl :)
     
  4. angstr0m

    angstr0m

    Joined:
    Jan 18, 2010
    Posts:
    178
    Good work on the new Path system.

    But when I try to use the path and the detonator explosion framework package in one project I get the following error:

    Code (csharp):
    1.  
    2. Assets/Detonator Explosion Framework/System/Detonator.cs(366,35): error CS0117: `Resources' does not contain a definition for `Load'
    This error are taken from a new project with only Detonator Framework and Path imported.

    I used the actual versions of both packages from the asset store.
     
  5. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Ah. Sorry about that. I see I didn't put Path in a namespace for this release. Quite silly. Will fix.

    In the mean time, what you can do for fixing it is, in each file giving you that error, add the following to the top:
    Code (csharp):
    1. using Resources=UnityEngine.Resources;
    Again, sorry about not wrapping this up in a namespace. I'll have a release out soon addressing this.
     
  6. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Is Path strictly a waypoint-based system, or does it have the capability to generate navmeshes?
     
  7. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Path does not have built-in navmesh generation or navmesh pathfinding. Would support for pathfinding on a hand-made or elsewhere generated navmesh be of much interest?
     
  8. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Personally, I would say so, yes :)

    My ultimate pathfinding solution would enable me to give destination move/rotation commands to AI units and have them find their own way to said destination without getting stuck, sans the limitations of raycast-based pathfinding, and with as little setup as possible :)

    But that's probably a pipe dream, no?
     
  9. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Actually no. You've got something to look forward to in the updated Path integration with UnitySteer I'm shipping with Ricardo sometime at the end of this week / start of next week.
     
  10. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Awesome! As long as there's some decent explanation on how to set it up :) I've looked into UnitySteer before, but gave up trying to integrate it due to documentation being virtually nonexistent.
     
  11. thcsquad

    thcsquad

    Joined:
    Feb 16, 2011
    Posts:
    2
    I tried importing Path (the latest 2.02f from the Asset Store) into my project (Unity 3). The import itself seems to work, but when attaching the Navigator to any GameObject (blank or existing), the GUI doesn't show up in the inspector. There's no error messages or anything, but the only thing that appears in the inspector is the script reference itself, plus the 'Seeker Iteration Cap' setting.

    When I create a new project with only Path (per the tutorial video), it does show up, so I can confirm that my download isn't corrupted or anything. Something in my project is screwing it up, but it's hard to tell what that is when there are no error messages. Could this be the namespace issue already mentioned?
     
  12. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Path should not be affected by the namespace issue - it should only affect other scripts in your project. You're sure that the log is empty and that you do not have the inspector in debug mode?

    I also assume you meant the "Navigation" component and not the "Navigator", right?
     
  13. hellcats2

    hellcats2

    Joined:
    Jan 4, 2011
    Posts:
    16
    My first reaction is that we need a way of selecting and moving waypoints directly with the mouse instead of always using the pop-up menu or the > and < buttons. One possibility would be to add temporary gameobjects as children under the Navigation game object. Then you could even multi-select and move them all at once with the built-in support in the Unity editor. These temp objects would kill themselves at game startup, or just live in an inactive layer. Otherwise, I'm liking it.
     
  14. Zyxil

    Zyxil

    Joined:
    Nov 23, 2009
    Posts:
    111
    Perhaps using iTweenPath?
     
  15. thcsquad

    thcsquad

    Joined:
    Feb 16, 2011
    Posts:
    2
    Yeah, I meant Navigation. Not sure why I said Navigator. And nothing indeed showed up in the Console or even the editor log. I wasn't even aware that the Inspector had a debug mode, but I turned it on and the same thing showed up, only with one private variable, Waypoints, greyed out, added.

    I then thought that I should try moving the /Path/Editor folder to /Editor, replacing what was in /Editor (nothing). I succeeded in getting a bunch of Detonator errors, similar to what Angstrom was seeing. This gives me hope in thinking that perhaps Unity just wasn't seeing it in the folder before. Still no progress, though. Nothing shows up when adding a Navigation component to anything.

    I might just wait until you release the new one, though, if it is indeed coming soon. I plan on integrating UnitySteer, so it sounds perfect!
     
  16. hellcats2

    hellcats2

    Joined:
    Jan 4, 2011
    Posts:
    16
    I just took a look at the iTweenPath editor class. He's creating a series of PositionHandles, one for each node. This would work, but it does sort of clutter the scene with lots of handles. Still, an easy solution.
     
  17. Zyxil

    Zyxil

    Joined:
    Nov 23, 2009
    Posts:
    111
    Yeah, it's a bit quick and dirty, but it gets the job done. Maybe add a GizmoVisible toggle or something.
     
  18. hellcats2

    hellcats2

    Joined:
    Jan 4, 2011
    Posts:
    16
    It appears that Waypoint is an actual GameObject (you can duplicate one if you have it selected and get a free floating Waypoint. It also must have the HideInHierarchy attribute). So it should be a simple matter of putting a mesh on it to make it selectable.
     
  19. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Waypoints are indeed components and when you create them via the inspector I set the hide flags to reduce clutter. I could make this optional if you would prefer it, but that doesn't really seem like a good fix.

    I'll see what I can come up with. Thanks a lot for your feedback.
     
  20. hellcats2

    hellcats2

    Joined:
    Jan 4, 2011
    Posts:
    16
    Angry Ant,

    I've been decompiling your code to understand it better (sorry for that). I have a couple questions:

    SeekerData computes the HScore by summing the old score with the new Connections H value. Isn't this a bug? HScore should be an estimate of the remaining cost to the goal, not the sum of those estimates from the start position. Summing from the beginning is what you want for GScore, not HScore.

    Also, the cost function for connections uses Euclidian distance squared. Summing those squared distances for G is not really comparable with the H value distance squared. I know doing the square root is expensive, but if G isn't computed accurately then the returned Path may not be minimal.

    I don't know what algorithm you are using, so I could just be completely confused. Sorry to decompile and complain.
     
  21. draknir_

    draknir_

    Joined:
    Sep 1, 2009
    Posts:
    30
    Unfortunately this doesn't really solve the problem, it clashes with the other 'Resources' in the global namespace. I replaced all Resources.Load with UnityEngine.Resources.Load for now, works fine.
     
  22. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    @hellcats
    The seeker is getting some fixes for the next release. Although I should point out that decompilation is prohibited by the license.
     
  23. hellcats2

    hellcats2

    Joined:
    Jan 4, 2011
    Posts:
    16
    AngryAnt,

    Sorry about decompiling. I didn't actually read the license but I did read your note on your reasons for building code into assemblies on your blog so I thought you were ok with it (although I guess I did send you a funky bug-report!). Won't do it again.
     
  24. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    So AngryAnt, how easy will it be to implement a bot that follows the player and avoids obstacles once you and Ricardo integrate UnitySteer?

    Will I have to work my targeting logic into the UnitySteer vehicle scripts? Or will I be able to write my own script and easily send commands to them, eg:

    If(outside of attack distance){ seek and avoid target; }
     
  25. dissid

    dissid

    Joined:
    Dec 16, 2010
    Posts:
    41
    Ah, downloaded the 2.0 and it broke UnitySteer.. Have to read your blog first next time.
     
  26. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    @legend411
    The planned interface is simply you slap on avoidance and path following behaviours, set targetPosition from some script and your character starts moving.
     
  27. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    When i set values and start play, it works but then the values in the inspector resets itself immediately even if its still running or stopped. And i got an array index out of bounds error at some point.
     
  28. scinfu

    scinfu

    Joined:
    Oct 23, 2008
    Posts:
    404
    there is an example project ?
     
  29. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    @Aubergine:
    Be sure to untick the "targetSelfOnPlay" property. The index out of bounds exception is harmless and happens in the editor UI. That is already fixed in internal builds of Path 2.0.1 which will include a bunch of other fixes and additions and be out shortly.

    @scinfu:
    I'm working on a shared Path and Behave example project. Until then, there is the example seeker script on the Path download page.
     
  30. scinfu

    scinfu

    Joined:
    Oct 23, 2008
    Posts:
    404
    thanks looking forward example

    in DemoSeeker i do not understand why it not draw the complete path .

    anyway thanks for the very rich resource .

    ... Scinfu ...
     
  31. n0n4m3

    n0n4m3

    Joined:
    Oct 15, 2008
    Posts:
    8
    Hi Emil,
    first let me thank you for this great library

    Now to my problem, I'm trying to use it on a project that loads different scenes at runtime. These scenes have separate navigation scripts and this causes a problem that only one navigation is permitted. Will you had support for having different navigation scripts or how would you recommend to solve this problem?

    Thanks!
     
  32. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    How does the problem manifest itself in your scenario? How does it fail?

    Generally, if you want to keep an object around throughout gameplay, but keeping data between levels, you should use DontDestroyOnLoad. However the Navigation component should detect the presence of another instance and just destroy itself.
     
  33. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    I mean in the Navigation, i setup some waypoints then i click on autoscale and autoconnect. Blocking layers are all in place.
    First run, and it works. While running i check whats going on with the navigation inspector and every value has been reset. I stop playing, then all waypoints disappear.
     
  34. andererandre

    andererandre

    Joined:
    Dec 17, 2010
    Posts:
    683
    I really love the utilities you provide us, it's just great to see how many people around here know a lot about what they're doing and share their knowledge with the community. I'm currently thinking about whether I should write my own pathfinding script or use yours (which is very advanced and could fit my needs just perfectly). Just a quick question on the license, is it possible to show the unmodified logo in the main menu instead of the splash screen? As I'm still on Unity Indie it would be a pain to look at both the unity logo AND your logo at startup, it might take 10-15 seconds until the user is finally in the main menu.

    Don't get me wrong, I appreciate that you provide us this first class pathfinding kit for free, but I would even pay for it instead of having to wait over 10 seconds for the main menu to appear. The long waiting time for both Unity and your script could push me into writing my own (simplified and quite possibly a lot worse) AI pathfinding script as it's a huge problem for indie devs in my opinion. As I said showing it in the main menu or while the level is loading wouldn't be a problem at all as I could use this time for some asset loading in the background.
     
  35. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    Any word on this update? I downloaded 2.0f2 version from the Asset Store, but this issue still persists.
     
  36. Arno

    Arno

    Joined:
    Mar 10, 2010
    Posts:
    6
    I was looking foreward to the updat for unity3.

    The problem is iám getting an error i don't understand.

    ArgumentOutOfRangeException: Argument is out of range.
    Parameter name: index
    System.Collections.Generic.List`1[Waypoint].get_Item (Int32 index)
    PathInspector.OnNavigationGUI (UnityEngine.Object target)
    PathInspector.OnInspectorGUI ()
    UnityEditor.InspectorWindow.OnGUI () (at C:/BuildAgent/work/f724c1acfee760b6/Editor/Mono/Inspector/InspectorWindow.cs:364)
    System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)

    could you please help?
     
    Last edited: Mar 2, 2011
  37. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    So is there no way via your path API to obtain the list of WP's in the path as an Vector3[] array ? I have my own code for travelling along a path for my AI... just need the path.

    EDIT: Don't worry, found a way by iterating though Segments... would be nicer if you exposed a method that returns the Vector3 array though :)
     
    Last edited: Mar 3, 2011
  38. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    Am i the only one who gets the waypoint data disappear and reset itself after first run?
     
  39. Gaspedal

    Gaspedal

    Joined:
    Mar 29, 2009
    Posts:
    376
    I have tested it but I'm going back to Arons Pathfinding because I'm not friend of Splash Screens (and for four seconds!! + showing in credits + for commercial projects the author wants royalties per title). It is only a Pathfinding and one of the many assets for unity and when all asset-developers wants showing a splash screen, then the players would wait some minutes for playing the game. So I boycott assets with those conditions. This asset is wrong on the asset-store with this license conditions! Asset-Store should keep selling the assets easy without different license conditions like Turbosquid.

    I appreciate your pathfinding kit for free, but you should offer a (logo free) commercial version without royalties on the asset-store. We should not exaggerate and not torture us with a lot of crazy license-conditions. :)
     
    Last edited: Mar 6, 2011
  40. cerebrate

    cerebrate

    Joined:
    Jan 8, 2010
    Posts:
    261
    I have a problem with the pathfinding. In your documentation, and according to monodevelop's assembly browser, if there is no layermask set as the block layer, then it should be finding the closest node and doing pathfinding along nodes.

    This is not the case. Even if I set the navigator block layer to nothing, it will just calculate a direct line of site from the current position to the end position. (with or without 'use shortcuts' enabled)
     
  41. sasquotschi

    sasquotschi

    Joined:
    Jan 5, 2011
    Posts:
    24
    Can I Somehow duplicate the "Waypoint Network" to a second Navigation Object? If i make a Prefab out of my Navigation Object all Waypoints are deleted
     
  42. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    @plzdiekthxbye
    Please use http://angryant.com/licensing for alternative licensing requests.

    @Aubergine
    Values disappearing sounds really strange. Can you figure out exact reproduction steps for this? With regards to your waypoints disappearing, that sounds related. Your waypoints are not gone, but their gizmo rendering is controlled by the Navigation component. Entering and exiting playmode with or without the Navigation GO selected could possibly affect this. If not, relaunching Unity should do the trick. Again, I'm really interested in some reliable reproduction steps on this.

    @Seon
    As you can imagine, we've been pretty busy with 3.3 and GDC lately. I was hoping to be able to get 2.0.1 out before we really hit crunch, but that unfortunately did not work out. I'm shipping this as soon as possible.

    @Arno
    That exception (as mentioned earlier in this thread) is harmless and merely cosmetic. It'll be fixed in the coming release.

    @Seon
    Good idea about the Vector3 list utility method. Although it would completely ignore widths and radius, clearly it has many use cases.

    @Gaspedal
    I don't know where you read that I'm claiming any royalties. Certainly not in the license text.

    If four seconds takes too much out of your game and your time is too precious to go and research the possibility for alternative license conditions on my website rather than post an annoyed reply to my main project thread, please go ahead and use alternative solutions.

    @cerebrate
    Unfortunately this is a known issue with the current release. Regardless of "take shortcuts" status, the seeker will try a direct route from A to B at the beginning of the run - using whichever layer mask is set up on the Navigator. A fix for this is on the way.

    @sasquotschi
    The waypoints do not belong to the Navigator, but in stead exist in the scene as GameObject. I see the point in your use case and will make the auto-hiding of newly created GameObjects optional.
     
  43. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Ant, when can we expect to see the release with the UnitySteer integration? :)
     
  44. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    @legend411
    I'm coordinating this with Ricardo at the moment, matching release schedules and such.
     
  45. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    Yeah very strange indeed and looks like it only happens to me. All i do is import your package, create some waypoints, do autosize and autoconnect with some values and click start, nothing else. As soon as i click start, values turn back to defaults and all data gets lost.

    Then i got mad with this thing and wrote my own very basic waypoint based pathfinder and same thing happens with my own script. As soon as i start the game my Generic Lists get empty. Which i suppose is the case with yours as well.
    Relaunching or anything else doesnt prevent it from happening.
     
  46. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Are anyone else able to reproduce Aubergines issue? Sounds like a local setup problem if other scripts are affected.
     
  47. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    Would you kindly send me a working package with a very basic scene please? Very awkward that it only happens to me, probably its somethin i do?
     
  48. kici

    kici

    Joined:
    Dec 10, 2010
    Posts:
    44
    MAJOR PROBLEM, please consider fixing this asap... as its interfeering with alot of other things:

    You have something named Resources in the global namespace, and it's interfering with anything which uses Resources from UnityEngine
    It gives me alot of problems when i include this, and i have to change other scripts in order to make it work...

    <mike_mac> "He's not the Messiah, he's a very naughty boy"

    ahaha :)) okay thanks !
     
    Last edited: Mar 11, 2011
  49. asger

    asger

    Joined:
    Apr 6, 2006
    Posts:
    38
    Hi
    I really appreciate you took the time to develop this system, very smooth and easy to use., and it seems to be working well without errors for me.
    I think this has been requested before, but I would like to put a +1 on these two features:
    - The ability to select the waypoints in the editor
    - The option to set a maximum length for a connection
     
  50. kici

    kici

    Joined:
    Dec 10, 2010
    Posts:
    44
    AngryAnt, please let us know when you fixed the issue i stated above...