Search Unity

Poly|Nav: Pathfinding for Unity2D

Discussion in 'Assets and Asset Store' started by nuverian, Jan 27, 2014.

  1. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    $polynav.jpg

    Hello people,

    I wanted to make an easy to use 2D pathfinding system for the Adventure Game Framework I am working on as to define walkable and non-walkable areas. Working more on that part, made up for Poly|Nav...

    Poly|Nav is a polygonal based 2D pathfinding solution using A*, that is extremely easy to setup and use!
    You just define walkable and obstacle polygons easily, add the provided agent component and it's ready.
    The map can dynamicaly update and the API is very similar to unity's NavMeshAgent. You just need to call SetDestination(position) and the agent will pathfind it's way.

    It is made and ment to be used with Unity's new 4.3 2D system


    Here are two demonstration videos.







    Asset Store

    Documentation
     
    Last edited: Apr 23, 2018
    LunaTrap and Gozdek like this.
  2. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Alright then :)
    PolyNav is live on the Asset Store. With this chance I've made a new video showcasing PolyNav for adventure games usage! Updated the initial post above with the video.
    I might do an isometric example as well
    Have fun
     
  3. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    nice work's !
     
  4. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Hey thanks :)
     
  5. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Is there anyone interested in PolyNav though would like a C# version?
     
    twobob likes this.
  6. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    I'd say its more that you should have a C# version! 68% of Unity users are C# users!
     
  7. NilConstantin

    NilConstantin

    Joined:
    Feb 18, 2014
    Posts:
    3
    Hello!
    Sorry for my English.

    Yesterday I bought your component. Nice stuff!
    But I have some questions.
    1. Offset from edges.


    2. Agents don't avoidance each other like in 3d NavAgent.

    Any simple built-in ways? Do you planning something of that in nearest future?
     
  8. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Hey there. I'm glad you like it :)
    Yes agent radius (offset from edges) is in my next to do and will be available on the next update. I didn't have plans for avoidance between one another, but I will look into it right after the agent radious.
    I hope it be done soon. Please hang on :)
     
  9. NilConstantin

    NilConstantin

    Joined:
    Feb 18, 2014
    Posts:
    3
    Nice!!
    I've also been experimenting a bit with the navigation's code, if I've got something interesting - I can share with you :)
     
  10. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    PolyNav is now available is C# :)

    The next immediate feature which I will submit tomorrow is agent offset as suggested by NilConstanin above.
    If you already have done work with PolyNav, the JS version is also included, since due to the port the previous scripts will no longer work.
    Enjoy :)

    @NilConstanin Im glad to hear that. Thanks
     
    twobob likes this.
  11. finking4

    finking4

    Joined:
    Dec 24, 2013
    Posts:
    6
    Hey, really like the look of this and tempted to purchase it, few quick questions though.

    with the "ClickToMove" script does it work with mobile touches?

    thanks.
     
  12. AndreasGan

    AndreasGan

    Joined:
    Feb 27, 2014
    Posts:
    16
    Hey,
    I'm wondering if you're planning on making it possible to make automatic obstacles? Also, make new ones at runtime?
    I recently started with Unity, and for my first real project I'm working on a RTS/Tower Defense sort of a game, and I need to make new obstacles when a structure is placed.

    Thanks,
    - Andrew

    Edit: Is there a way to make polynav ignore colliders from other objects? I have a grid made up of quads, I need colliders on them for a ray. Currently my character can't move into the grid-area, because of the colliders.
     
    Last edited: Mar 3, 2014
  13. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Hello and thanks,

    The ClickToMove script included is an extremely simple example. It's actually this:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. //example
    5. [RequireComponent(typeof(PolyNavAgent))]
    6. public class ClickToMove : MonoBehaviour{
    7.    
    8.     private PolyNavAgent _agent;
    9.  
    10.     public PolyNavAgent agent{
    11.         get
    12.         {
    13.             if (!_agent)
    14.                 _agent = GetComponent(typeof(PolyNavAgent)) as PolyNavAgent;
    15.             return _agent;         
    16.         }
    17.     }
    18.  
    19.     private void Update () {
    20.  
    21.         if (Input.GetMouseButtonDown(0))
    22.             agent.SetDestination(Camera.main.ScreenToWorldPoint(Input.mousePosition));
    23.     }
    24.  
    25.     //Message from Agent
    26.     private void OnDestinationReached(){
    27.  
    28.         //do something here...
    29.     }
    30.  
    31.     //Message from Agent
    32.     private void OnDestinationInvalid(){
    33.  
    34.         //do something here...
    35.     }
    36. }
    37.  
    So all you will have to do is to replace the ' if (Input.GetMouseButtonDown(0)) ' with the respective Input.GetTouch functions. All you have to do is call SetDestination on the PolyNavAgent Component specifying the target position.

    I hope it helps. I can provide a full script if you like :)

    Hey there,

    It is possible to create obstacles at runtime. Any game object with the PolyNavObstacle Component attached is automaticaly treated. So for example if you spawn such a gameobject at runtime, the navigation map will change and update automaticaly. The same will happen when you destroy, disable or move/rotate/scale such an obstacle. Is that what you mean? :)

    Yes, there is a way to make PolyNav ignore other colliders although I must admit, its not very obvious so I will fix it in the next update.
    For now, all you have to do is to move the game objects with colliders you want to ignore in the z axis to be greater than that of the main PolyNav2D gameobject.
    In short, just put their Z value greater than 10 and thats it. :)
     
    Last edited: Mar 4, 2014
  14. AndreasGan

    AndreasGan

    Joined:
    Feb 27, 2014
    Posts:
    16
    Hey,
    Ah! Didn't see that one, Thanks a bunch! :D

    Haha, a bit random, but it works, so thanks :D

    Edit: Hey again,
    Apparently if the object with the 'agent' script "can't see its path", it won't move? It's only happening with my sprites that use "PolyNavObstacle", though.
    Video: http://youtu.be/VRWjUwDNogY
     
    Last edited: Mar 5, 2014
  15. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Hey,
    I can't seem to reproduce the issue. What is your setup of the wall objects? Can you please post a screen shot of one of those?
    Do they have the PolyNavObstacle and and if they do please check that they are in the correct layer which they should be automaticaly be added if you have selected a layer in the PolyNav2D.

    $pathfinding.gif
     
  16. monkolabs

    monkolabs

    Joined:
    Feb 12, 2014
    Posts:
    6
  17. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Hello,
    Can you please specify what you mean by Dynamic Layer?
     
  18. AndreasGan

    AndreasGan

    Joined:
    Feb 27, 2014
    Posts:
    16
    Hey,
    Through quite a lot of testing, I finally found the problem.
    If you add a "PolyNavObstacle" script to an object which already has a shape, it will act like in the video in my post.
    I tried removing the spriterenderer, it didn't work. But if I create an empty sprite (GameObject->Create Other->Sprite), and add a PolyNavObstacle, it works as intended, BECAUSE it has no sprite attached to it, therefore no shape.

    Sorry if that wasn't clear enough, tried my best =[
    Anyways, do you have to use polygoncolliders? Why can't you just use any collider?
    Collider2D collider; if(collider == null) collider = GetComponent<Collider2D>(); //in PolyNavObstacle.cs

    Another thing; in C# the default access modifier is private, so you don't have to type private. however, you do have to type public! That means you have to add 'public' before the "invertPolygon" variable in polynavobstacle.

    Hopefully you find a fix for this, I'll try to look a bit, but I have other things to do.
    Thanks for the support,
    - Andrew

    Edit: nevermind the attachment.. Don't know how to delete it.
     

    Attached Files:

  19. monkolabs

    monkolabs

    Joined:
    Feb 12, 2014
    Posts:
    6
    a little extra if you have a build time error to android :)

    just add # if UNITY_EDITOR and # endif code.

    on PolyNav2D.cs:

    Code (csharp):
    1. # if UNITY_EDITOR
    2. using UnityEditor;
    3. # endif
    and

    Code (csharp):
    1. # if UNITY_EDITOR
    2. [MenuItem ("GameObject / Create Other/PolyNav2D")]
    3. static void CreatePolyNav () {
    4. if (FindObjectOfType (typeof (PolyNav2D)) == null) {
    5. PolyNav2D newNav = new GameObject ("@ PolyNav2D"). AddComponent <PolyNav2D> ();
    6. Selection.activeObject = newNav;
    7. }
    8. }
    9. # endif
    hopefully help.
     
    Last edited: Mar 6, 2014
  20. monkolabs

    monkolabs

    Joined:
    Feb 12, 2014
    Posts:
    6
    like that in the video demo, detect if the character behind the obstacle then changed his oreder layer behind the layer obstacle, and vice versa.

    $1.PNG
    $2.PNG

    thanks. :)
     
    Last edited: Mar 6, 2014
  21. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Hey nice catch. Actually if there is already a polygon collider automaticaly created due to a sprite renderer, then the InvertPolygon has to be checked, but me forgetting it to private deafeted the purpose :p. I've made it automaticaly to be set correctly now.
    Yeah I know that access modifiers are in reverse to UnityScript, and due to being used to UScript I now declare everything explicitely even if private, but I forgot that one. sorry

    Regarding using any collider it wont just work as it is now. The polygon collider has points defined, but I can make it so to work with box collider as well in the next update no problem.

    Thanks

    For the purpose of the video it was done roughly, but I've send you a PM with 2 script you can generaly use which I've made for my AGF project. I might add them to PolyNav as they are quite relevant actually.

    PS: Thanks for pointing out those. I've totaly forgot to add if UNITY_EDITOR there :/
     
  22. monkolabs

    monkolabs

    Joined:
    Feb 12, 2014
    Posts:
    6
    Done !!! Thanks you very much.. :D
     
  23. AndreasGan

    AndreasGan

    Joined:
    Feb 27, 2014
    Posts:
    16
    Thankyou thankyou thankyou THANK YOU! It works! Now I can finally make my dream! haha :D
    I'm very happy now x)

    PS. You should probably make people aware of the "invertpolygon"! I can't possibly be the only one who needs this? :) Maybe rename the variable.

    oh, and I thought of a feature that would be really cool: instead of stopping when destination is invalid, make it walk as close to the destination as possible? Don't worry, you don't have to, but it would be a great bonus :)
    - Andrew
     
    Last edited: Mar 7, 2014
  24. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Hey thanks a lot! I'm glad I made you happy :p

    That invert variable should be more clear, I agree.

    About that feature, it should be trivial to add, since there is already a GetCloserPointToEdge function. I will make use of it and add the feature :)

    Best regards,
    I wish your dream come true :p
     
  25. Oz1571

    Oz1571

    Joined:
    Mar 10, 2014
    Posts:
    1
    I just wanted to say that I have bought a handful of Unity assets before, all of which I am very satisfied with, but this is the best asset so far. I have had no problems integrating it into my top-down game. The code is very easy to read and expand on, and I was generating a navmesh from my 2D Toolkit tilemap in an evening's work.

    I'm looking forward to the edge offset being implemented. :)
     
  26. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Yep I actively don't buy any js only assets. I don't make any exceptions, I check first that it's C# and has source :)

    If it's js only, I simply make it myself.
     
    twobob likes this.
  27. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Thanks a lot for your kind comments. I am glad it works well for you :). Offseting is comming soon along with some more improvements.


    PolyNav is now in C# with full source code included as of the last version uploaded. Just letting you know :)
     
  28. AndreasGan

    AndreasGan

    Joined:
    Feb 27, 2014
    Posts:
    16
    Hey!
    I PM'ed you, because I have some problems yet again :p

    Thanks for the support,
    - Andrew
     
  29. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Just submited the new version with the most requested feature, agent offseting from edges.
    I've also added an option on the PolyNavAgent to go to the nearest possible position if the requested one is invalid instead of stopping.

    I hope it goes live soon. Let me know of any other features you'd like PolyNav to have :)

    Thanks
     
  30. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    The new version is now live. Have fun and let me know if you have any requests :)
     
  31. finking4

    finking4

    Joined:
    Dec 24, 2013
    Posts:
    6
    Hey, loving this asset so far but have come to a issue with colliders in that i that i need them to detect when my agent has passed through a doorway for example but my agent sees this other collider as a boundary and cant walk through it.

    so my question is.

    Is their a way to sort of ignore colliders from the polyNav so the agent can move through it setting it off as a trigger and not be classed as a boundary/obstacle?

    thank you
     
  32. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Hello and thanks :)
    If you dont want a collider to act as an actual collider, you can as well completely turn it off (the collider component, not the gameobject). The navigation will still work as it should. Is that what you mean? :)
     
  33. finking4

    finking4

    Joined:
    Dec 24, 2013
    Posts:
    6
    thanks for the reply, i need the collider to work normally like for hitting objects or detecting when a object passes through it.
     
  34. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    No problem :) I'm just trying to understand the requirements :)
    Is what you need to disable a PolyNav obstacle but keep the Polygon Collider?
     
  35. finking4

    finking4

    Joined:
    Dec 24, 2013
    Posts:
    6
    i have a 2D building that is layered so that when the player walks to certain areas layer go transparent. i am using polyNav to navigate around these buildings / environment and need to use normal colliders for physics and triggers for other gameplay aspects but they are being affected by polyNav making my agent unable to walk through them, as i guess polyNav sees other colliders as obstacles like the red polys polyNav obstacles.

    sorry i really dont know how to explain this differently
     
  36. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Oh alright. I understand your issue now. Sorry I should have thought.
    Have you upgraded to the last version? That was an issue happening before that version where the solution was to move your non nav polygons further back in z than the PolyNav2D, but in the last version that doesnt happen and colliders are not being considered at all and as such you should not have that problem.
    Please let me know if you are on the last version of PolyNav
     
  37. finking4

    finking4

    Joined:
    Dec 24, 2013
    Posts:
    6
    I haven't downloaded polyNav again since when i first did it 3 weeks ago. so i most likely on the old version, i cant access the asset store where i am at the moment so will have to test when i get home and will report back.

    Thank you for your time :D
     
  38. finking4

    finking4

    Joined:
    Dec 24, 2013
    Posts:
    6
    Thanks Nuverian the update fixed my issues.

    Thanks again and great asset
     
  39. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Nice. Glad to know everything is ok.
    Thanks a lot :)
     
  40. Burletech

    Burletech

    Joined:
    Jul 1, 2010
    Posts:
    73
    Is there agent avoidance in this package? Like if there are three enemies pathing will they all cluster on the same node or will they move for each other? Thanks.
     
  41. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Hello,
    There is no agent avoidance in PolyNav yet, but it's on the roadmap.
    Sorry :)
     
  42. Survie

    Survie

    Joined:
    Jan 31, 2014
    Posts:
    1
    Just wanted to drop by and say my thanks again! After the latest update PolyNav is truly perfect.
     
  43. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Thank you very much! :)
    I'm glad you enjoy it.

    I really want to find time and add agent avoidance and off-'mesh' links. Hope they come in sooner rather than later :p

    Thanks again!
     
  44. ezsilman

    ezsilman

    Joined:
    Feb 25, 2014
    Posts:
    2
    This is a great asset! I'm trying to make it so that when I click outside of the PolyNav area it goes to the edge of the PolyNav closest to the position of the mouse click. I'm just a bit stuck. Do you know how I could achieve this?

    **

    EDIT: Sorry, I just noticed the CloserPointOnInvalid boolean. Woohoo thanks.
     
    Last edited: Apr 12, 2014
  45. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Thanks! Sorry I've missed your post, but I see you figured it out :)

    By the way I want to share the brief documentation for anyone to see. So here it is as well as updated the first post.

    Documentation
     
  46. Cancermantis

    Cancermantis

    Joined:
    Oct 16, 2013
    Posts:
    2
    Is there any kind of built in functionaliy at the moment for having some agents ignore certain obstacles but not others? I want to have a type of unit that can move through "low" obstacles, which can be shot over, while the other units are stopped by those obstacles. But I also wanted some solid obstacles that can't be shot over and that nothing can walk over. With time I'm sure I can come up with a solution but I figure I should try to avoid reinventing the wheel if I can
     
  47. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Hello,

    Unfortunately there is no build is way for doing this, and I can't come up with an easy way of going about it as well. I will think some more on that and if I come up with a solution I will prodvide that here.
    Sorry :)
     
  48. chansey97

    chansey97

    Joined:
    Sep 21, 2012
    Posts:
    59
    Hi nuverian,
    I want buy your plugin, but I have some questions:
    1. Is this plugin works in xy plane?
    2. Can this plugin support navmesh dynamic change? (eg: navmesh cut)

    thanks.
     
  49. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Hello there,

    Yes the pathfinding works in XY plane.

    The second question is a bit harder. I am not sure what you mean by "navmesh cut". :)
    What I can let you know though, is that you can move/rotate/scale path obstacles around and the navigation map will update dynamicly and so will the agents/characters, to find their new path around.

    Is that what you mean, or something different?

    Thanks :)
     
  50. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Hey there again,

    Each pathfinding implementation works differently.
    With PolyNav you can have dynamic obstacles able to move, rotate and scale them around regardless of how it's implemented.
    This is demonstrated in the first video in the first post in this thread, at about minute 1:00.

    :)