Search Unity

Navigation2D Official Thread - Use Unity's built in Pathfinding for 2D Games

Discussion in 'Assets and Asset Store' started by mischa2k, Jan 4, 2017.

  1. PigFruit

    PigFruit

    Joined:
    Sep 13, 2017
    Posts:
    2
    Hello,

    I just purchased this asset and realized that I need to be baking the navmesh at runtime as I'm trying to use this for a randomly generated dungeon. I forgot to check if I can do that before purchasing the asset.

    Can I bake the Navmesh at runtime and if so, is there any documentation for how I might?

    Thank you
     
  2. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Not yet. I will add support for that once Unity adds official support for that with the built in Navigation system.
    Their new navmesh runtime baking systems are on github somewhere already.
     
  3. PigFruit

    PigFruit

    Joined:
    Sep 13, 2017
    Posts:
    2
    Awesome so could I use the navmesh baking at runtime from the system on GitHub then? I'm very happy to hear that you're planning on updating it to feature baking at runtime once unity adds support for it.
     
  4. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    I haven't looked at the new feature yet. You might abe able to adapt it to Navigation2D, or maybe not.
    Will be added once it's part of Unity natively.
     
  5. Kaven-Co-Games

    Kaven-Co-Games

    Joined:
    Jun 3, 2014
    Posts:
    19
    Hello. I have a large map of tiles and have have created a mesh of colliders on them to save on performance.

    I'm realizing now that your asset isn't working with my map because my map is just 1 object. Do you have any suggestions on how to continue? I could just use simple Manhattan pathfinding, but I want the flexability of a navmesh, so my characters don't have to move along strict paths.
    screenshotOf2dColliders.png
     
  6. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    not sure what I am seeing there, but maybe polygon colliders work?
     
  7. Kaven-Co-Games

    Kaven-Co-Games

    Joined:
    Jun 3, 2014
    Posts:
    19
    Hmm... Is there a way to bake these navmesh maps in runtime? or must it be done in the editor?
     
  8. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Not at the moment. Unity has some new navigation tools on github somewhere that allow runtime baking, but I won't support that until it's natively in Unity. Maybe you can try those tools and try to modify Navigation2D to work with it
     
  9. tinytim12

    tinytim12

    Joined:
    Jun 6, 2017
    Posts:
    2
    Hi, Thank you for the asset!

    I noticed that if the collider is tiled, when the navmesh is baked the full collider won't be detected, only the base collider.

    Will you be fixing this in the future?

    Thanks :DDDD
     
  10. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    What do you mean?
    Can you tell me how to reproduce it?
     
  11. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Just wondering, do you think this could be related to the issue I raised in Post #78 (point 2 in that post)?
     
  12. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Tilemap colliders definitely work, I use Nav2d in uMMORPG2D without issues.
    There might be edge cases though, who knows. So please let me know how to reproduce it.
     
  13. Larry-Dietz

    Larry-Dietz

    Joined:
    Apr 22, 2010
    Posts:
    21
    I am trying to use Nav2D with a SmoothFollow script on the camera to keep the character in frame. However, as soon as I started moving the camera, the character stopped moving to the point I clicked.

    I am setting the NavAgent destination to the point of the click with the following code..
    Code (CSharp):
    1. Vector3 w = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    2.             My2DNavAgent.destination = w;
    Any idea what I am doing wrong here?

    -Larry
     
  14. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Looks fine. Compare with the built in example scene to see how it works.
     
  15. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Hi tinytim12, you will need to provide vis2k with a simple cut down example demonstrating your issue.

    If your issue is having a tile generator program that creates polygon colliders that contain more than one element, then it is the same issue that I pointed out in Post #78 in this thread (point 2 in that post). :)
     
  16. Larry-Dietz

    Larry-Dietz

    Joined:
    Apr 22, 2010
    Posts:
    21
    It is a match for the builtin example. And the example scene works fine, as the camera is station in the example. If I remove the camera follow script on my scene, it works fine as well. The problem is that it stops working when the camera moves, and I don't understand why ;(

    I'll keep plugging away at it. I'll eventually figure it out, or switch to a different navigation system.
     
  17. Larry-Dietz

    Larry-Dietz

    Joined:
    Apr 22, 2010
    Posts:
    21
    Another question.

    I have a scene with a single box collider. With the character on one side of the box collider, I click on the opposite side, expecting him to walk around the box. However, more than half the time, the IsStuck code kicks is, and I get the following in the console...

    Code (CSharp):
    1. stopped agent because of collision in 2D plane
    2. UnityEngine.Debug:Log(Object)
    3. NavMeshAgent2D:Update() (at Assets/Navigation2D/Plugins/NavMeshAgent2D.cs:84)
    4.  
    What would cause this? The whole point of the navigation mesh it so an object can find a path around obstacles. Looking at the navmesh, there is more than enough room to walk around the box, and if I click enough times, eventually it finds a path around it, but there has to be something to make this work more reliably.

    Any help is appreciated,
    -Larry
     
  18. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Please compare with the example scene. It works there. Maybe it's a scale problem or something else. The error appears if the built in 3D NavMeshAgent gets stuck somewhere.
     
  19. tinytim12

    tinytim12

    Joined:
    Jun 6, 2017
    Posts:
    2
    Hi, sorry for the late reply. I am a newbie to Unity so it will be difficult to explain so please bear with me.

    I'm not using a tile generator program, but using Unity's normal auto tiling. So normally I have one collider on a gameobject, but since that gameobject is comprised of tiles, I have an option to "auto tile" it. Autotiling it separates it into more colliders. When autotiling, the navmesh detects only the collider BEFORE it was autotiled, and does not detect any additional colliders after the autoiling
     

    Attached Files:

  20. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Maybe try without auto tiling?
     
  21. Gerrickg

    Gerrickg

    Joined:
    Sep 20, 2013
    Posts:
    3
    First I love the asset thanks for all the work. I'm wondering if you have had a chance to look into Behavior Designer. I'm seeming to have an issue with how to make the behavior tree "movement work with Nav2D mesh.
     
  22. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    I have not, sorry
     
  23. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    I don't think that's possible with the built in NavMeshAgent either
     
  24. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
  25. zeus911

    zeus911

    Joined:
    Nov 1, 2016
    Posts:
    1
    While its probably not the fault of your code, I'm receiving this error message right after importing:
    "Assets/Navigation2D/Plugins/NavMeshAgent2D.cs(155,28): error CS1061: Type `UnityEngine.AI.NavMeshAgent' does not contain a definition for `isStopped' and no extension method `isStopped' of type `UnityEngine.AI.NavMeshAgent' could be found. Are you missing an assembly reference?"
    I checked and isStopped should be in the UnityEngine.AI.NavMeshAgent... Do you have any guess what might cause this?
    Thank you
     
  26. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Which Unity version do you use? Sounds like an older one? They renamed this property a while ago. I think it was called paused before?

    Try Unity 2017.4 LTS, that's really good version!
     
  27. MercurialKid

    MercurialKid

    Joined:
    Aug 30, 2018
    Posts:
    22
    Bake not working. 2018.2.10f1. I'm all setup with a project using this version. Using just the tilemap collider. I've simplified the colliders to rectangles.

    upload_2018-9-28_12-1-22.png

    upload_2018-9-28_12-2-57.png

    What am I missing, please? The only way I get this to work is by adding box colliders individually to each platform.
     

    Attached Files:

    Last edited: Sep 28, 2018
  28. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Probably easiest if you do add boxcolliders to each one for now.
    If you can send me the project (as simplified as possible) then I can also try to debug it.
     
  29. MercurialKid

    MercurialKid

    Joined:
    Aug 30, 2018
    Posts:
    22
    Adding them is not possible, there are hundreds. The asset states that it supports TileMap Collider, which in this simple case, it doesn't. I'm not in a position to start start packaging up projects, as ours is complex, I'd prefer a refund, thanks. I'll contact you by email shortly.
     
  30. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    It does support TileMap colliders. This works perfectly fine in my uMMORPG 2D asset, used by hundreds of people.
    But there are always edge cases that might have been missed. Maybe I didn't consider a scale or a rotation or a position offset somewhere.
    It's fine if you want a refund, no problem. But if you can add a few sprites into an empty project and reproduce it there then I might be able to fix it too.
     
  31. MercurialKid

    MercurialKid

    Joined:
    Aug 30, 2018
    Posts:
    22
    I've made a test project; which fails too. Nothing much 3 tiles, a player a camera. I'll send by email now. Please let me know asap if you can see anything obvious, thanks.

    ** edit - your asset seems to work with box colliders and polygon colliders fine in my tests: but it doesn't work with Tilemap collider (as stated). I know Unity are actively developing this area. I can't change Unity version as I'm relying on exactly this development and want to keep my project current whilst it's in development. Hopefully you can find in debug what's causing the issue.

    ** Sent to info..noobtuts.zyx
     
    Last edited: Sep 29, 2018
  32. gabrielpm3491

    gabrielpm3491

    Joined:
    Feb 7, 2018
    Posts:
    9
    Hello Vis2k. I bought Navigation2D for use in my game but I have a problem. I would like the movement to be by Grid Tile, that is, only in X and Y without diagonal and always stop in the center of the tile. Very common in most 2D games such as Tibia.
    I found two examples on the Unity wiki but I'm not sure how I can integrate with Navigation2D.

    http://wiki.unity3d.com/index.php/MeshCreationGrid
    http://wiki.unity3d.com/index.php/GridMove#C.23_-_GridMove.cs


    If someone can help me, thank you very much.
     
  33. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Modify your player's movement script to always set agent.destination = (1,1) or (2,3) etc
     
  34. gabrielpm3491

    gabrielpm3491

    Joined:
    Feb 7, 2018
    Posts:
    9
    OK. I'll try to do.
    Thank you.
     
  35. gabrielpm3491

    gabrielpm3491

    Joined:
    Feb 7, 2018
    Posts:
    9
    Good night Vis2k.
    Sorry but I could not do it like you said. I tried to make but it accuses an error.

    Could you show me what the code looks like?

    Code (CSharp):
    1. void Update() {
    2.         if (Input.GetMouseButton(0)) {
    3.             Vector3 w = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    4.             GetComponent<NavMeshAgent2D>().destination = w;
    5.         }
    6.     }
     
  36. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Did you read the error?
     
  37. gabrielpm3491

    gabrielpm3491

    Joined:
    Feb 7, 2018
    Posts:
    9
    I think I'm doing it wrong. To use the keys I did succeed doing so.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.SceneManagement;
    4. using System.Collections;
    5.  
    6. public class PlayerBehaviour : MonoBehaviour
    7. {
    8.  
    9.     public float speed = 2.0f;
    10.     Vector3 pos;
    11.     Vector3 old;
    12.     Transform tr;
    13.  
    14.     void Start()
    15.     {
    16.         pos = transform.position;
    17.         tr = transform;
    18.     }
    19.  
    20.     void FixedUpdate()
    21.     {
    22.         if (Input.GetKey(KeyCode.UpArrow) && tr.position == pos)
    23.         {
    24.             pos += (Vector3.up) / 1;
    25.         }
    26.         else if (Input.GetKey(KeyCode.RightArrow) && tr.position == pos)
    27.         {
    28.             pos += (Vector3.right) / 1;
    29.         }
    30.         else if (Input.GetKey(KeyCode.DownArrow) && tr.position == pos)
    31.         {
    32.             pos += (Vector3.down) / 1;
    33.         }
    34.         else if (Input.GetKey(KeyCode.LeftArrow) && tr.position == pos)
    35.         {
    36.             pos += (Vector3.left) / 1;
    37.         }
    38.  
    39.         transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
    40.     }
    41.  
    42.     void OnCollisionEnter2D()
    43.     {
    44.         float x = transform.position.x;
    45.         float y = transform.position.y;
    46.         float z = transform.position.z;
    47.  
    48.         if ((int)(x + 0.5) > (int)x && x > 0)
    49.             x = (int)x + 0.5f;
    50.         else if ((int)(x - 0.5) < (int)x && x < 0)
    51.             x = (int)x - 0.5f;
    52.         else
    53.             x = (int)x;
    54.  
    55.         if ((int)(y + 0.5) > (int)y && y > 0)
    56.             y = (int)y + 0.5f;
    57.         else if ((int)(y - 0.5) < (int)y && y < 0)
    58.             y = (int)y - 0.5f;
    59.         else
    60.             y = (int)y;
    61.  
    62.         z = 0f;
    63.  
    64.         Vector3 away = new Vector3(x, y, z);
    65.         pos = away;
    66.  
    67.     }
    68. }
     
    Last edited: Oct 7, 2018
  38. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    If your code doesn't run then debug it :)
    Check one line after another, see where it goes wrong.
     
  39. gabrielpm3491

    gabrielpm3491

    Joined:
    Feb 7, 2018
    Posts:
    9
    Good afternoon Vis2k. Sorry but I could not get it to work and I really need help on how to do it. I did not understand what you wanted to say with (agent.destination = (1,1) or (2,3) etc). It could show how I have to leave the code. Maybe I'm going through the biggest job to change a simple thing. Thank you.
     
  40. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Check out the Unity docs on NavMeshAgent (not 2D). See how to set your own path. Then set your own path :)
     
  41. gabrielpm3491

    gabrielpm3491

    Joined:
    Feb 7, 2018
    Posts:
    9
    Hello Vis2k. Well what I tried to do was this:

    Code (CSharp):
    1. // project agent position to 2D
    2.     public static Vector2 ProjectTo2D(Vector3 v)
    3.     {
    4.         return new Vector2((int)v.x, (int)v.z);
    5.     }
    I tried to put it as (int). Even worked but the character walks with the screen freezing in every move.
     
  42. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    I think you might need to set your own path there
     
  43. gabrielpm3491

    gabrielpm3491

    Joined:
    Feb 7, 2018
    Posts:
    9
    But I do not want to set a path. I just need the character to always walk in the center of the tile and not diagonally.
     
  44. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    And it doesn't do that by default, so you need some kind of WalkOnTile(Vector3 destination) function that calculates and sets a custom path :)
     
  45. gabrielpm3491

    gabrielpm3491

    Joined:
    Feb 7, 2018
    Posts:
    9
    OK. The method I have this below:

    Code (CSharp):
    1.     Vector3 pos;
    2.     Vector3 old;
    3.     Transform tr;
    4.     void Start()
    5.     {
    6.         pos = transform.position;
    7.         tr = transform;
    8.     }
    9.  
    10.     void WalkOnTile()
    11.     {
    12.         float x = transform.position.x;
    13.         float y = transform.position.y;
    14.         float z = transform.position.z;
    15.         if ((int)(x + 0.5) > (int)x && x > 0)
    16.             x = (int)x + 0.5f;
    17.         else if ((int)(x - 0.5) < (int)x && x < 0)
    18.             x = (int)x - 0.5f;
    19.         else
    20.             x = (int)x;
    21.         if ((int)(y + 0.5) > (int)y && y > 0)
    22.             y = (int)y + 0.5f;
    23.         else if ((int)(y - 0.5) < (int)y && y < 0)
    24.             y = (int)y - 0.5f;
    25.         else
    26.             y = (int)y;
    27.         z = 0f;
    28.         Vector3 away = new Vector3(x, y, z);
    29.         pos = away;
    30.     }
    But now where do I put it and where should I call the method?
     
    Last edited: Nov 22, 2018
  46. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    You can put it for example in an extension function for NavMeshAgent2D.
     
  47. FlexMentallo

    FlexMentallo

    Joined:
    Nov 15, 2018
    Posts:
    1
    Hey Vis, I bought Nav2D and it worked great for my top down 2D project with the default Unity tilemap stuff. I had a map with all the collidable tiles in one map and the non-collidable floor in another map.

    After reading good reviews of STME and that it worked with Nav2D I picked that up today and rebuilt my test level in it and set up the collidable tiles. All the collision is working correctly from the player POV, but Nav2D isn't generating nav cut around the collision anymore - it just generates one sheet of contiguous nav across the whole tilemap.

    Any ideas if there's something dumb and obvious I might have missed?
     
  48. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Make sure that the tiles have colliders enabled
     
  49. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Check the extends property in Window->Navigation2D
     
  50. OJ2KOfficial

    OJ2KOfficial

    Joined:
    Jan 4, 2019
    Posts:
    2
    Hey, Every time I Bake a navmesh it always goes outside the confinements of the sprite and over them as if there was a hole.