Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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
    Latest version of PolyNav also includes NodeCanvas Tasks and Playmaker Actions!
    Both package support are attached here for anyone that wants them before new version goes live.

    With both package supports, you can
    • Set an agent destination to a position
    • Set an agent destination to a game object position
    • Get the remaining distance
    • Get the current destination
    • Check if a point is a valid walkable point
    • Check if a path exists from point A to point B
    • Stop the agent

    Have fun :)
     

    Attached Files:

  2. phamish

    phamish

    Joined:
    Mar 18, 2013
    Posts:
    2
    Hmm, I suspected as much, thanks for the quick reply.
     
  3. Lagnas

    Lagnas

    Joined:
    Apr 27, 2014
    Posts:
    18
    Hello. Long time no see. Through you, my project advanced. :)

    But, I have a problem with PolyNav2D. If make PolyNav2D prefab with player object and instantiate other scene, then error with below message.

    NullReferenceException: Object reference not set to an instance of an object
    PolyNav2D.CreatePolyMap () (at Assets/Script/PolyNav2D/PolyNav2D.cs:151)
    PolyNav2D.OnDrawGizmos () (at Assets/Script/PolyNav2D/PolyNav2D.cs:417)
    UnityEditor.DockArea:OnGUI()


    I think that, require 'reset and search other obstacles on scene process'.


    Thank! ;)
     
  4. nuverian

    nuverian

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

    The NavObstacles register themselves to PolyNav2D OnEnable and removed OnDisable, but indeed if you instantiate PolyNav2D it will not search existing NavObstacles. I will fix that in the next version once the current gets finaly approved

    Cheers :)
     
  5. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    The new PolyNav version 1.3 is live on the asset store :)

    To recap, here are the changes:
    • First implementation of agent avoidance :)
    • Increased performance when regenerating map.
    • There is no longer the need to define obstacle layers.
    • The edge collider is no longer automaticaly created.
    • NodeCanvas integration tasks.
    • Playmaker Integration actions.

    Cheers and have fun!
     
  6. Lagnas

    Lagnas

    Joined:
    Apr 27, 2014
    Posts:
    18
    Wow! New PolyNav2D is More faster than old version.

    I worried about FPS, because if many obstacle generated, sometime occur lack.
    But new PolyNav2D is more faster than old version.

    Old version FPS : 40~65 FPS
    New version FPS : over 60 FPS

    And, other small bugs (like edge collider or doesn`t regenerate @PolyNav`s edge) are fixed.

    Thanks!!!
     
  7. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Glad you like the new version and that you noticed the difference :)
    Thanks as well.
     
  8. alienwoods

    alienwoods

    Joined:
    Oct 11, 2010
    Posts:
    30
    Hi,

    I bought your navigation script but i have 1 issue.
    I use it with a big map, where there is 2 bigger collider navigation obstacle.

    My problem is, every time when the agent goes trough from a corner to a narrow path, it slighty stops a bit, so the speed is not constant.
     
  9. nuverian

    nuverian

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

    If the agent is comming along with relative high speed into a narrow path, it will slow down to be able to take the turn 'realisticaly', else it will just bump onto the wall ahead :)
    I'm not sure how you imagine the agent taking that tight turn when it has relative high speed (?)
    I mean, it has to slow down at some point.
    You can play around with the agent's properties in the inspector especially mass, to slightly change that behaviour
     
  10. alienwoods

    alienwoods

    Joined:
    Oct 11, 2010
    Posts:
    30
    Thanks, i changed the path layout and its more better now, but here is another which i donno is it a bug yet or not :)

    I build my game to iOS, iPad, with the "Closer point on invalid" parameter turned on in the inspector view, on my PolyNavAgent script.
    However it works on Android, but on iOS build it still get the click from the path area only!

    Could you please be kind and tell me some solution to fix this?
     
  11. nuverian

    nuverian

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

    That's very weird since PolyNav doesn't use anything unsuported by some platform.
    Are you calling SetDestination on the Agent yourself through code? If so can you confirm with a Debug.Log that your destination position is in both cases (Android/IOS) the same?

    Thanks
     
  12. alienwoods

    alienwoods

    Joined:
    Oct 11, 2010
    Posts:
    30
    Yes, i use the agent's SetDestination() to trigger the character movement, and also i found out this option (Closer point on invalid) only works when i build for ios is a Development Build.

    When i disable the Development build option, this feature simply stop working, and it detects only inputs that are inside the path area.
     
  13. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Hello, Sorry for late reply. Summer :)

    I'm not so sure if that's the issue, but please try this:

    In PolyNav2D.cs replace line #381 (OrderBy) with the following:
    Code (CSharp):
    1.  
    2.         var closerDist = Mathf.Infinity;
    3.         var index = 0;
    4.         for (int i = 0; i < possiblePoints.Count; i++){
    5.             var dist = (point - possiblePoints[i]).magnitude;
    6.             if (dist < closerDist){
    7.                 closerDist = dist;
    8.                 index = i;
    9.             }
    10.         }
    11.         return possiblePoints[index];
    12.  
    Let me know if this works.
     
  14. BadFoolPrototype

    BadFoolPrototype

    Joined:
    Sep 7, 2013
    Posts:
    14
    Hey man fantastic plugin!
    I've discovered just a flaw, easly resolvable.
    If you are using the points from a PolygonCollider2D, the array will give you only the points of the FIRST PATH.
    If instead you cycle of the path and get the points of that path, you get everything working:
    Code (CSharp):
    1. List<Vector2> reversedPoints = new List<Vector2>();
    2.         for ( int i = 0; i < masterCollider.pathCount; ++i ) {
    3.             for ( int p = 0; p < masterCollider.GetPath( i ).Count(); ++p )       {
    4.                 reversedPoints.Add( masterCollider.GetPath( i )[p] );
    5.             }
    6.         }
    7.  
    8. reversedPoints.Reverse();
    I had a case that was not working, but just substituting this and in the DrawGizmo everything worked again!
     
  15. BadFoolPrototype

    BadFoolPrototype

    Joined:
    Sep 7, 2013
    Posts:
    14
    Actually it needs a little change - the Polymap should not inherit from polygon but has a new list of polygons, one for each polygon collider path.
    Changed that and it works perfectly!!!
     
  16. nuverian

    nuverian

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

    Um, The code you posted before doesnt seem right. I mean, there is no GetPath method somewhere in there :)
    What are the issues you encounter?

    Cheers!
     
  17. alienwoods

    alienwoods

    Joined:
    Oct 11, 2010
    Posts:
    30

    Hi!

    Thx a lot it works now for IOS in non-development build mode ;)
     
  18. spiceboy9994

    spiceboy9994

    Joined:
    Sep 17, 2012
    Posts:
    6
    Hi. I'm evaluating different navigation systems for a new game that I'm designing. That game is a Top Down/ 2d / RTS game. My two options are Pathfinding and navmeshes. I see this plugin is really simple from the design point of view. However, my game is going to add obstacles on runtime, so I may need to add polygon obstacles in runtime. Is this possible with this framework?. Do you have a video for that?

    Thanks
     
  19. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Hello, you can very well add/remove or transform obstacles at runtime and the navigation will dynamicaly update.
    You can check the first video with the spaceships where at about the middle of it, I dublicate the existing obstacle while the game is running, which is the same as instantiating a new one :)

    Let me know if you have any further questions.

    Cheers
     
    Last edited: Aug 16, 2014
    spiceboy9994 likes this.
  20. BadFoolPrototype

    BadFoolPrototype

    Joined:
    Sep 7, 2013
    Posts:
    14
    Hey Nuve, the issue I have are with complex polygonal colliders that ends up to have more than one path.
    The GetPath is a method in the PolygonCollider2D:

    http://docs.unity3d.com/ScriptReference/PolygonCollider2D.html

    you can have more than one path and thus you need to handle that :)

    What was happening is that with a complex navmesh, the character could not simply move because it was stuck to the first path.
    I already fixed everything and it works now, if you want I can send you the code!
     
  21. BadFoolPrototype

    BadFoolPrototype

    Joined:
    Sep 7, 2013
    Posts:
    14
    This is an example of navmesh that have more than 1 path

    upload_2014-8-19_2-27-0.png
     
  22. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    ooh... Now I see what you mean :)
    You are right!
    Those polygons should be handled, yes.
    I would really appreciate if you want to send me your fix!

    Thanks!
     
  23. ezsilman

    ezsilman

    Joined:
    Feb 25, 2014
    Posts:
    2
    nuverian, could you please add the "Platform Click To Move" script you have added to your player character as shown in the Monkey Island demo video? It would also be great to see how you built in the dynamic sorting layer functionality according to whether the character is in front of or behind an object.
     
  24. Goodev

    Goodev

    Joined:
    Feb 19, 2013
    Posts:
    25
    I found some bugs.
    On runtime scripting, If I chaged PolyNavObstacle enable/disable value, It's not curretly works.
    ex)
    1. Spike bject has many child with PolyNavObstacle.
    2. Spike scripting enable/disable every child's object( using for loop )
    3. Some works fine, But others Not work.

    Plz check.
     
  25. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Hello,
    The same ClickToMove behaviour is used for the monkey island video. The only difference here is the way tha the sprite get's animated and fliped.
    I can PM you these if you want along withe the "dynamic" sorting layers scripts :)

    Cheers!

    Hello,

    Sorry, I didnt understand what you mean :(
    If you disable/enable a PolyNavObstacle at runtime, the navigation map will regenerate. Just make sure that "Regenerate On Chage" is checked on the PolyNav2D inspector (first option).

    Please let me know what's the problem, or send me a project if that is possible so that I can check it.
    Thanks!
     
  26. Goodev

    Goodev

    Joined:
    Feb 19, 2013
    Posts:
    25
    Thanks for reply, and I'm sorry, I'm poor at English.

    here's simple example.
    If I disable many PolyNavObstacle on same time.
    Some object are not removed polynavmap.

    I guess..
    Maybe, PolyNav2D::GenerateMap is called many times, but return by generateFlag is true

    P.S. You need to import PolyNav package.
     

    Attached Files:

    Last edited: Aug 22, 2014
  27. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Hello,
    Sorry for late reply.
    I've fixed this issue and send to Asset Store. Let me know if you want the fix sooner
    Thanks
     
  28. Goodev

    Goodev

    Joined:
    Feb 19, 2013
    Posts:
    25
    PolyNav version is 1.3.2 (Jul 10, 2014) in AssetStore.
    How can I recieve fixed PolyNav?
    If you can send mail recent version, I will wait and appreciated.
    goodev1015@gmail.com
     
    Last edited: Aug 28, 2014
  29. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Hello,
    Version 1.3.3 in now on the Asset Store, which contains the fix :)
    Cheers!
     
  30. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    The new version 1.3.4 is submited, mostly fixing some issues:

    • Multiple polygon paths are now taken into account (SUPER thanks to @BadFoolPrototype !)
    • Paths are now retrieved muuuch faster
    • Fixed null reference error when creating new obstacles
    • Fixed enabling/disabling multiple obstacles in same frame error
    Cheers and as always..have fun!
     
    4848 likes this.
  31. superking12

    superking12

    Joined:
    Apr 7, 2014
    Posts:
    1
    Hey there, I'm working on a top down game using 2D Toolkit. I was wondering, could you elaborate on how you managed to generate a navmesh using a tilemap? And did you also get obstacles generating?

    Thanks,

    Tim
     
  32. CHEZ-HUNG

    CHEZ-HUNG

    Joined:
    Oct 18, 2013
    Posts:
    2
    I guess someone must have asked the question but I didn't seem to find it, sorry if it's just me overlooked.
    Do I need Unity Pro to use PolyNav?
     
  33. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Hello there.
    PolyNav works woth both Unity Free & Pro :)

    Cheers!
     
  34. SmellyIrishMan

    SmellyIrishMan

    Joined:
    Jul 2, 2013
    Posts:
    5
    Does PolyNav allow for varying agent sizes? As far as I can tell it does not.
    For example in this image, the blue circle can get to the green X since it is small enough, while the red circle is blocked.

     
  35. nuverian

    nuverian

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

    No, at this point PolyNav doesn't have this feature. The agent size can be set globaly for all agents as far as pathfinding goes and localy for avoidance.
    This feature may come along in the future.

    Cheers :)
     
  36. SmellyIrishMan

    SmellyIrishMan

    Joined:
    Jul 2, 2013
    Posts:
    5
    Cool. Thanks for the quick reply!
     
  37. Puavo

    Puavo

    Joined:
    Jan 30, 2013
    Posts:
    2
    Just bought this with the assumption that agent avoidance works so that agents actually calculate paths around other agents, as if they were obstacles. This doesn't seem to be the case? I'm kind of bummed :/
     
  38. nuverian

    nuverian

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

    Paths are not actually calculated around other agents, rather agents avoid one another based on vector forces. Calculating a path like it's done on obstacles would be too heavy for performance since there could be many agents which may move constantely.

    Are you not satisfied with the current implementation?
    Cheers
     
  39. xpxilom

    xpxilom

    Joined:
    Aug 28, 2014
    Posts:
    30
    You can do anything, if an agent is stopped covering a road, find another way available?


    and if several agents are in the same place and I are just a ajente seized up, seeking another way?

    Or some sort of aggregate for agents including function?


    Thank you! Very good trabjo! Thanks in advance, Best Scrip! You are great!
     
  40. Alathon

    Alathon

    Joined:
    Feb 10, 2013
    Posts:
    3
    Hi Nuverian,

    I'm not 100% sure this has anything to do with Poly|Nav specifically, but.. help!

    I'm working on a top-down 2D game in Unity3D, using the 2D standard setup. This means that its sprites wandering about, and I've followed your documentation to add polynav obstacles. And it works the way it should, awesome.

    *** However *** I've run into an issue with collision. So here's the setup:

    'Player' GameObject, which is a sprite, so: SpriteRenderer, Rigidbody 2D and PolyNavAgent (and a C# script to move with arrowkeys to test).

    The sprite is tall, so normally it would have collision only at its feet. Before I found PolyNav, I gave it a BoxCollider only around its feet, and that worked properly. But it seems like Poly|Nav doesn't care about this with the collision polygons; they only stop when the center of the sprite hits the boundary. See screenshot:



    The sprite is visible behind the camera, and notice that its standing *in* the lava, because collision seems to occur with the middle of the sprite... Here's a more clear screenshot with the camera moved, where I re-added the BoxCollider (but still using Poly|Nav collision obstacles).


    Am I missing something obvious here??
     
    Last edited: Oct 1, 2014
  41. Alathon

    Alathon

    Joined:
    Feb 10, 2013
    Posts:
    3
    To simplify this a little:

    Collision seems to happen based solely on the pivot of the 2D sprite. Is it possible to change this somehow, such that I can define a rectangle or polygon of my own to collide based on?
     
    Last edited: Oct 1, 2014
  42. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Hello,
    Thanks a lot!
    I will see to improving the agent avoidance :)
    Cheers!

    Hello,

    PolyNav doesn't actually use physics for anything. If you take a look you will see that there is no Rigidbody component added anywhere and all polygon colliders are set to IsTrigger. As such it doesn't get in your way, when making/handling the physics for the game.

    The reason that agents are can not enter a polygon area, is because the agent is "forced" to stay outside them.
    As you correctly figured out, this is based on the agents pivot point, or in other words, it's transform position.

    The way that you should go about it, is to set the pivot of the sprite at the implied middle of it's base. So somewhere between her feet verticaly speaking, and then set the global radious through the PolyNav2D component and as such offseting the polygon map and along with it the valid areas within any agent can "walk".

    Let me know if you want an image of what I mean :)

    Alternatively you could set up your physics like you normaly would, in which case you will need to add a Rigidbody component to the agent, along with a collider to make your normal setup. If you go this way and you want to use the polygons for collision you will also need to make sure that they are not triggers (which is by default). In this case please open up PolyNavObstacle.cs and comment out line #56 which set the isTrigger to true OnEnable. (I will make this an option in the next version)

    Cheers!
    let me know if you have any questions :)
     
  43. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    With your next update can you make the PolyNavAgent velocity public or add a property to it? I have a couple of situations that need to know the current velocity of the agent. It's no rush and for now I am just multiplying agent.movingDirection by agent.currentSpeed but that's an unnecessary multiplication for something that is already calculated.

    Also, from your ClickToMove example, I know that it isn't used but OnDestinationReached needs to have a bool parameter :)
     
  44. Alathon

    Alathon

    Joined:
    Feb 10, 2013
    Posts:
    3
    Thanks for the reply =) Yeah, I figured out how to change the pivot point in the Sprite Editor and that helped. Ideally I would've liked to be able to specify a width & height instead of a radius, but I do realize that the way you currently do things that might not be entirely possible as-is.

    A different question altogether: I'm moving my 2D game to use 1 pixel per unit, but it seems Poly|Nav expects PixelPerUnit to be 100? The poly nav objects created through the GUI are miniscule by default, at least. Can you clarify whether this is on my end or on purpose? and if on purpose, could you add a 'Pixel Per Unit' setting somewhere?
     
  45. Puavo

    Puavo

    Joined:
    Jan 30, 2013
    Posts:
    2
    I actually tried making agents also obstacles, which worked in a way... but the agents moved all over the place, because they were not only calculating a path around the obstacles of other agents, but also of their own (as they were instances of the same prefab). Anyway, I did notice that it can be heavy for performance if there are a lot of agents moving around.

    However, I think it would be great if there was at least an option to make agents calculate paths around other agents. Even better, it would be awesome if you could toggle it on and off from code, so that it could be used only in specific situations, to avoid poor performance.

    Thanks for the quick reply, by the way!
     
    Last edited: Oct 4, 2014
  46. gumboots

    gumboots

    Joined:
    May 24, 2011
    Posts:
    298
    Hi there!

    I'm currently trying to implement Polynav into my project, and I'm finding that my wall colliders just won't work as obstacles.

    They register in the @Polynav2D asset, and you can click 'Select' and it'll select that game object, but the path finder just walks right through the walls.

    There are a number of Polygon collider paths on the object, but I read on the forum that you added compatibility for this.

    Any help is appreciated!



     
  47. nuverian

    nuverian

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

    Sure I will make a public property for velocity.
    What do you mean regarding OnDestinationReached? The message is not send with any parameter.
    A bool parameter is though used if you call SetDestination with a callback which denotes if the destination is reached or not successfuly.
    PolyNavAgent.SetDestination(Vector2 goal, Action<bool> callback)

    Hello again,
    I will take a look at what you are suggesting, but I can't promise anything yet :)
    PolyNav doesn't fiddle at all with PixelPerUnit. When adding a new Obstacle, the Polygon added to it is just the default polygon component with it's default settings. There is no special handling of anything there :)

    Yes, it will be quite heavy if you treat agents as navigation obstacles in the same way as the PolyNavObstacles are treated. I will try and improve the current avoidance implementation though.

    Thanks :)


    Hello,

    Having lot of polygons on the same gameobject like this will not work :) Only one is taken into account.
    The support for multiple paths you've read is for when the same PolygonCollider2D has more than one path, which is something that can be achived only through code (as far as I know).

    So ,the way to do this is to have different PolyNavObstacle game objects per obstacle you want to exist.
    Thanks :)
     
  48. opsive

    opsive

    Joined:
    Mar 15, 2010
    Posts:
    5,122
    Appreciate it! For OnDestinationReached, I just looked and that was my mistake. I was confusing that with the SetDestination callback.
     
  49. gumboots

    gumboots

    Joined:
    May 24, 2011
    Posts:
    298
    Hi again!

    So I've compressed the PolygonCollider2Ds down to a single component, with multiple paths. Unfortunately the AI still ignores the walls. When I click on the Obstacle, this is what it creates:



    And it actually ignores all the other paths:



    And here's the component:



    Any help is appreciated!
     
  50. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Hello,
    Somethings weird is going on :)
    Please, send me a project so that I can take a look at it if possible.
    Thanks in advance!