Search Unity

Poly|Nav: Pathfinding for Unity2D

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

  1. Bubagent86

    Bubagent86

    Joined:
    Jan 3, 2014
    Posts:
    10
    Hi, I just bought this last night, and I seem to be having a problem where if I double the scale of an obstacle, the size of the area around the obstacle that the agent won't enter doubles in size as well. Do you know what's going wrong?

    Thanks,
    -Gabe
     
  2. Bubagent86

    Bubagent86

    Joined:
    Jan 3, 2014
    Posts:
    10
    Also I'm working in boo (I'm the one guy), and I need the polynav scripts to compile prior to my own code, is there any way to move the polynav folder into standard assets?
     
  3. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,051
    Hello Gabe,

    Thanks!

    Make sure that 'Generate On Update' is true on the PolyNav script. This will regenerate the map whenever a PolyObstacle moves, rotates, or scales. :)

    Regarding using it from Boo or JS, you can move the 'Editor' and 'Runtime' folders found under PolyNav2D/Scripts in a folder named 'Plugins' and it should work.
    You can possible then rename them as well to be more descriptive, something like 'PolyNavEditor' and 'PolyNavRuntime'

    Let me know :)
     
  4. Bubagent86

    Bubagent86

    Joined:
    Jan 3, 2014
    Posts:
    10
    Moving the sub-folders work fine, though the other problem doesn't seem to be affected by whether or not the map regenerates each frame. Par exemplar:
    $unity image.png

    Notice how the right edge is given a wider berth than the one on top.
     
    Last edited: May 12, 2014
  5. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,051
    Oh now I see what you mean.
    This seems to happen if the obstacle is not uniformly scaled, It's a bug in the offset radious. Nice catch :)
    I will need to take a deep look at this. I will post back with a fix tomorrow first thing (as it's getting reeealy late here)..
    Sorry about that!
     
  6. Bubagent86

    Bubagent86

    Joined:
    Jan 3, 2014
    Posts:
    10
    No worries, thanks for the prompt reply.
     
  7. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,051
    Hello again,

    So,I've found and fixed the problem and submited it.
    Please enable PM so that I can send the fix to you if you need it sooner.

    Thanks
     
  8. Bubagent86

    Bubagent86

    Joined:
    Jan 3, 2014
    Posts:
    10
    Just enabled it. Thanks for your help!
     
  9. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,051
    Fix PM send. You are welcome :)
     
  10. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,051
    version 1.2.1 in live.
    This version simply fixes a bug found by Bubagent86 above, where the offset radious from the edges were scaled as well when the polygon object was scaled.
     
  11. grechzoo

    grechzoo

    Joined:
    Mar 16, 2014
    Posts:
    5
    Hi there,

    I'm about twenty minutes from buying this tool as there really is nothing better from what i have seen in terms of support for the 2D side of unity.

    However I'm worried that all of my game so far has been written in CSharp. I don't mind writing the scripts needed for path setup and navigation in whatever native language this plugins uses, I just want to make sure I wont get any errors or problems, as every other scripts in my scene will be in C#.


    Also you mention in your documentation its important to have obstacles on a defined layer. does the "Obstacles Layer" simply refer to a normal unity layer? and does this mean every sprite on that layer will act as an obstacle to avoid.

    Or are you talking about the PolyObstacles that you can create and adjust?

    Hope you can answer my questions.
     
    Last edited: May 15, 2014
  12. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,051
    Hello there :)

    PolyNav is written in C#, so you won't been having any problems at all.
    Only the very first version was in JS, but then everything was ported and now is in C#.
     
  13. grechzoo

    grechzoo

    Joined:
    Mar 16, 2014
    Posts:
    5
    Thanks for the quick response.

    I did ask another question (my post was edited after your response ;))

    Here it is again:

    Also you mention in your documentation its important to have obstacles on a defined layer. does the "Obstacles Layer" simply refer to a normal unity layer? and does this mean every sprite on that layer will act as an obstacle to avoid.

    Or are you talking about the PolyObstacles that you can create and adjust?

    thanks
     
  14. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,051
    Sorry I missed your reply :/

    The PolyNavObstacles need to be on a seperate layer than the PolyNav map.
    The layer you select there is the layer that all PolyNavObstacles gameobjects will be set to.

    Yes, that refers to normal Unity layes. :)

    Thanks
     
  15. GoodAsNew

    GoodAsNew

    Joined:
    May 17, 2014
    Posts:
    2
    Hey there,

    Is it easy to setup this PolyNav so that it just returns the path but then i could use my own system to move the objects along the path? Maybe an array of waypoints or something?
     
  16. nuverian

    nuverian

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

    There is no special setup needed for that. The PolyNavAgent is optional, you don't have to use it. You can get the path in a List<Vector2> by calling:
    PolyNav2D.current.FindPath(Vector2 start, Vector2 end, Action<List<Vector2>> callback)

    Foe example:
    Code (csharp):
    1.  
    2. public class Example{
    3.  
    4.     void GetPath(Vector2 start, Vector2 end){
    5.  
    6.         PolyNav2D.current.FindPath(start, end, PathReady);
    7.     }
    8.  
    9.     void PathReady(List<Vector2> path){
    10.  
    11.         //Do something with the path
    12.     }
    13. }
    14.  

    Cheers :)
     
  17. GoodAsNew

    GoodAsNew

    Joined:
    May 17, 2014
    Posts:
    2
    Thanks for super fast response.

    I'll probably try this out soon. :)
     
  18. jimutt

    jimutt

    Joined:
    Aug 22, 2013
    Posts:
    4
    Hi!
    This asset really seems just great. I'm quite tempted to buy it, though I'm wondering how the implemention of agent avoidance is going? Because as I'm planning to maybe use this for a game with RTS style controls I would need that feature to make it look good. Keep up the good work :)

    EDIT: One more question. Are there any possibility of you adding a "penalty" feature? Which allow you to create "penalty" zones that agents are less willingly to move over. For example if you're building a strategy games where you want your units to move on roads instead of over tough terrain. Then the penalty on the terrain areas will make them choose the road if the distance isn't much shorter when choosing an other path.

    Just tell me if you don't understand what I mean. Though I think it would be very nice with a solution like that. To see what I mean you can have a look at this example: http://unityassetreview.com/demos/4076/astar-pathfinding-project-pro-penalties-and-tags
     
    Last edited: May 28, 2014
  19. codeitload-it

    codeitload-it

    Joined:
    Nov 21, 2012
    Posts:
    1
    Just wanted to add my support for the agent avoidance feature. That is the only thing holding me back from buying, as I too am doing an RTS style game and I can't have the units be on top of each other (overlapping.)
     
  20. Bubagent86

    Bubagent86

    Joined:
    Jan 3, 2014
    Posts:
    10
    Hi!
    So I seem to be having a problem where an agent will occasionally stop when reaching the end of his path for a long time before sending his completion message. I think this may be cause by the get closest point on invalid not taking into account the radius around the obstacles. Is this possible?
    Thanks again.

    P.S:
    One more vote for agent avoidance

    Edit:
    I need to do more testing but I believe that was the problem. It was a simple fix on in the closer point script in polynav2d.
    Edit:
    Upon further investigation this doesn't seem like it's completely remedied the issue. It's possible it exists somewhere in my code, but I doubt it.
     
    Last edited: May 30, 2014
  21. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,051
    Hello everyone,

    I see there's much demand for agent avoidance. Alright I will see what I can do and implement it then :)

    @jimutt Yep, I understand what you mean about area cost. I can't promise anything yet, but I will look into it :)

    @Bubagent86 I don't exactly understand what you mean it's going wrong :/
    Is it possible that you send me a project which this behaviour happens?
    Thanks
     
  22. Zelek

    Zelek

    Joined:
    Jun 12, 2010
    Posts:
    87
    I just wanted to chime in as one more person looking for a 2d pathfinding solution with local avoidance. Really looking forward to seeing the updated version of PolyNav.
     
  23. Lagnas

    Lagnas

    Joined:
    Apr 27, 2014
    Posts:
    18
    Wow, most creative method I think. :)

    But, I have some question before I buy. In your first Video(Poly|Nav: 2D Pathfinding for Unity ), you make new obstacle and agents repath. Can make agent repath when new 'Walkable' polygon?

    And, my project is 2D RPG based tile system, so path based tile System, too. Tile system make a tile with collider2D, and Instantiate one by one. Can mak new 'Walkable' polygon with new tile prefab?

    Thanks. :)
     
  24. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,051
    Hello Lagnas and thanks :)

    There is only one 'walkable' polygon at a time. All other polygons are obstacles. More walkable polygons is something I want to do along with local avoidance in near future.

    For your case scenario you could have a big walkable polygon (as big as the map can be) and have obstacle tiles have the obstacle component and a PolygonCollider2D. As soon as an obstacle tile is instantiated the pathfinding will update automaticaly.

    Thanks
     
  25. Lagnas

    Lagnas

    Joined:
    Apr 27, 2014
    Posts:
    18
    Thanks for your answer. I already bought this asset before your answer. I invented solution at my bed. ;)

    So, I solved my problem. I attached wide Walkable polygon to player object. Then, player move player object and wide Walkable polygon.


    This pathfinding method is best way for Unity2D I think. Because pathfinding method based mesh need 'Bake process', so could not applyed to interactive world(ex : making obstacle or destroy obstacle). But this is not to say that flip x-axis and use 3D collider, not 2D collider.

    I think that, this asset is very suitable method instead mesh base pathfinding or waypoint base pathfinding methood.


    Thanks for your asset. I will advertise this asset in Korean developer community. ;)
     
  26. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,051
    Thanks a lot for you feedback and kind comments :)
    I am very glad it suits your needs and find it usefull. Hopefully I'll manage to improve it it the near future!

    Best regards,
    Gavalakis
     
  27. Lagnas

    Lagnas

    Joined:
    Apr 27, 2014
    Posts:
    18
    I found one problem.

    PolyEdge Object (child of @PolyNav object) does not rotate. It rotate when at least one NavObstacle object attached @PolyNav object, so I made a NavObstacle (Scale X : 0, Y : 0) and attached @PolyNav.

    I think this is a bug. ;)
     
    Last edited: Jun 14, 2014
  28. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,051
    Oh :)

    I will take a look at it.
    By the way, you can completely deactivate that PolyEdge. It doesn't do anything for PolyNav itself, but it rather exists in case you want so for your physics.
    Still, I will need to fix that.

    Thanks.
     
  29. SpacePilot1000

    SpacePilot1000

    Joined:
    Dec 23, 2013
    Posts:
    7
    Hi nuverian, just wanted to say thanks for making this asset, it's very handy!

    I have one suggestion for improving performance when obstacles call GenerateMap() during their update call. If multiple obstacles change position in the same update, GenerateMap() will be called multiple times. This could be avoided by having PolyNav2D just set a flag that an update has been requested and then do the actual map rebuild during it's own Update() call (or something along these lines).

    Anyways, keep up the great work, it's definitely very cool!
     
  30. nuverian

    nuverian

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

    I'm very glad you like PolyNav :)
    You suggestion is very correct. I'll see in doing this.

    Thanks ;)
     
  31. Oruji

    Oruji

    Joined:
    Nov 5, 2012
    Posts:
    14
    Hi, firstly thanks for this great asset.

    With the default setup, if I click outside the boundaries or on an obstacle the path is discarded.

    I have a use case where I would like the player to be able to click outside of the polymesh or on an obstacle and the character would move as close as possible to the point of the click while staying within the boundaries.

    Do you have a suggestion of how to do that?

    Thanks
     
  32. nuverian

    nuverian

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

    Thanks as well:)

    There is an option on the PolyNavAgent component to do exactly what you want. It's called CloserPointOnInvalid.
    :)
     
  33. Oruji

    Oruji

    Joined:
    Nov 5, 2012
    Posts:
    14
    Ah fantastic :D
    Thanks!
     
  34. Kerel

    Kerel

    Joined:
    Nov 30, 2012
    Posts:
    9
    Is it suitable for platformer? I just didn't find any solution yet, and this looks promising :)
     
  35. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,051
    Hello!

    I see no reason why it shouldn't. I also remember someone here was working with it in a platform game.
    But of course it also depends on the kind of platform game :) Let me know if you have specific questions for the implementation or how you would go about setting it up for a platform.

    Cheers!
     
  36. zeteginara

    zeteginara

    Joined:
    Apr 26, 2013
    Posts:
    14
    I'm making an RPG with monsters that you can see who will try to pathfind to chase you.

    I'm using 2Dtoolkit which makes edge colliders out of the tiles, and this is the only pathfinding solution I can find that works with that.

    So, the map I'm using is rather large, and the unwalkable areas are very complex and have a lot of shapes. There are large treelines that have many alcoves that jut out, and kind of produce a 'stairstep' effect of walkable tiles, and some that kinda zigzag.

    I made very complex polygon colliders for these walls with many vertices (some of them 100+, and now my scene takes forever to load.

    Would it be better and faster to use many (many) smaller polygons with 3-5 vertices and put them together, replacing a 100+ vertices polygon collider with about 50 such smaller polygons?
     
    Last edited: Jul 3, 2014
  37. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,051
    Hello!

    Does the scene load takes forever due to the pathfinding or due to the many Unity Polygon Colliders?
    Generaly, it's better to have big polygons like you do, instead of many smaller ones.

    Do you maybe move/rotate or scale some of the objstacle polygons in Awake or Start. If so, the navigation area is recalculated everytime such a transform takes place if the 'Generate On Update' is checked on the PolyNav component. If so, try to uncheck that option and call PolyNav2D.current.GenerateMap() manually when you need so.

    Let me know.
    Thanks
     
  38. zeteginara

    zeteginara

    Joined:
    Apr 26, 2013
    Posts:
    14
    It takes forever to load due to the many polygon colliders. I have a lot. I have about 20 large ones with an average of 80 vertices, and about 80 smaller 4-vertices ones for obstacles. It's a large map.

    'Generate On Update' is unchecked on the polynav component.

    I'm not moving, rotating, or scaling any of the obstacle polygons in Awake or Start.

    I'm loading my scene additively, and it takes forever. Like literally a minute. If I disable the polynav colliders, the load time goes away.
     
  39. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,051
    Alright. So I just noticed something in code that could be the very issue for those long loading times and is very relevant to what SpacePilot said couple of posts above. That was bad in my part, so please hang on if you can and I will fix it tomorrow first thing.

    Please PM me your email and the invoice(sorry to ask about it) so that I can send it as soon as I have it fixed.
     
  40. zeteginara

    zeteginara

    Joined:
    Apr 26, 2013
    Posts:
    14
    I PMed you. Thanks!
     
  41. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,051
    I've send you the new version. Let me know :)


    ----------
    I've also sumbited version 1.3. Here is what's new:
    • There is no longer the need to specify layers. You can have your obstacles in any layer you want for your game.
    • The EdgeCollider is no longer automaticaly generated unless you require it.
    • Improved performance for when Generating map.
    • I've added a simple agent avoidance :). There is of course room for improvement.

    PolyNavAvoidance.gif
     
  42. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    876
    great i'am always interesed for isometric gaming but i fear that's any other exemple to see it's maybie one good idea
     
  43. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,051
    Sorry, I didn't understand what you mean :/ Can you rephrase that please?
     
  44. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    876
    about deep any way to scale on it
     
  45. Alekxss

    Alekxss

    Joined:
    Mar 25, 2013
    Posts:
    163
    Look like this is realy fantastic stuff!!!!

    How it will works with for example one big rect walkable area and, for example 1500 rect obsrtacles and about 30 active agents? I means speed :)

    And it's possible to rework it myself for 3D Top-down RTS game?
    (sorry for my English, i hope you can get it)
     
  46. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,051
    Hello and thanks :)

    PolyNav creates a pathfinding node at each convex point of an obstacle if that point is in the valid walkable area (eg no covered by another obstacle). So for 1500 rect polygons which by definition are convex, if they do not overlap at all, then 6000 nodes need to be created and then calucalted for pathfinding.
    The generation is not a problem, but pathfinding will be slow.
    If you were to have 2 obstacle rect tha sit exactly next to one another for example, then it will be better to group them into one bigger or slightly overlap them so 2 instead of 4 nodes are created in the middle.

    So in a few words, it won't perform great in your example situation :)

    It could be possible to use it with 3D, but the game will have to be created in the 2D plane (x,y)
     
  47. Alekxss

    Alekxss

    Joined:
    Mar 25, 2013
    Posts:
    163
    Thanks for your answer. So grid-based solution still be best for me. Good luck with your projects ;)
     
  48. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,051
    You are welcome :)

    For a grid based game, probably grid based pathfinding suits the case most.
    PolyNav is great for when you make games that have a more freeform art style.

    Cheers!
     
  49. phamish

    phamish

    Joined:
    Mar 18, 2013
    Posts:
    2
    Hi, I have really liked using your asset so far. I was wondering if there is a way to limit the directions used for pathfinding? For example I want an object that can only move in the cardinal directions (no diagonal directions) and still avoid objects.
     
  50. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,051
    Hello. Glad you like it :)

    Unfortunately what you are after can't be done unless you write your own pathfinding agent, which is certainly not an easy task. It's more a matter of how an agent moves along the path rather than how the path itself is calculated.
    And this can become even harder since PolyNav is not grid based.
    Sorry for the negative reply :)
     
unityunity