Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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. Myradorn

    Myradorn

    Joined:
    Nov 25, 2017
    Posts:
    3
    Thx for your answer, i wasn't clear enough. If dist > maxdist, then the function returns true, which means that the agent is stuck, but there is no reason for that.

    // stuck detection: reset if distance > max distance
    Vector2 transVec = new Vector2(transform.position.x, transform.position.y);
    float dist = Vector2.Distance(transVec, NavMeshUtils2D.ProjectTo2D(agent.transform.position));

    if(dist>maxdist)
    {
    Debug.Log(transform.position + ":" + NavMeshUtils2D.ProjectTo2D(agent.transform.position));
    float dist2 = Vector2.Distance(transform.position, NavMeshUtils2D.ProjectTo2D(agent.transform.position));
    Debug.Log(dist + ":" + maxdist);
    }

    The two Vectors transform.position and the NavMeshUtils2D Vector are exactly the same, in my case bothe are (1.6,3.2 ), the distance between those 2 should be zero, But Vector2.Distance returns 2,384186E-07, which is a really small distance but greater then 0, and because of that, the agent is stuck in the middle of the navmesh, it looks like a rounding bug somewhere in my opintion. For my case i found a workaround, so you dont have to answer, thx anyway but if someone else encounters my strange behavior.
     
  2. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,327
    What is your maxdist in that example?
     
  3. Myradorn

    Myradorn

    Joined:
    Nov 25, 2017
    Posts:
    3
    maxdist is 0 in my example, and dist is 2,384186E-07, for that dist is greater then maxdist and the agent return isStuck(). This means, that the console is flooded with the stopped agent because of collision in 2d plane.
     
  4. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,327
    maxdist shouldn't be 0. can you add some more debug.log messages to see which it is 0?
    it should be the collider size if I am not mistaken
     
  5. huttala

    huttala

    Joined:
    Dec 25, 2019
    Posts:
    2
    Hi,
    I'm having some troubles with tilemaps and the baking.It seems like the AddTilemapcollider2Ds method just checks if there is an collider inside the cell and makes the whole cell unwalkable. This is pretty limiting.

    Am I doing something wrong or is it working as indented?

    I'll add a picture for clarification.

     
  6. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,327
    In the Navigation2d code, comment out the line that destroys the GameObject that was created for 3D.
    Then after baking, check out the scene in 3D mode to see what it looks like.
    If it looks correct then it's Unity's baking settings that you need to adjust.
    If it looks false then it's my fault :)
     
  7. huttala

    huttala

    Joined:
    Dec 25, 2019
    Posts:
    2
    Thanks for the fast response!
    I'm assuming you ment that I should remove usage of the"MakeUnwalkable" method inside the AddTilemapCollider2Ds method?

    This did change the rendering, but not for the better I am afraid.

    The image below taken in 2d mode shows the problem clearly:


    And this is in 3d mode, looks the same:
     
  8. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,327
    I meant the agent size etc. settings in Window->Navigation2D. Play around with those.
    The result looks ok. it seems like that's just how Unity bakes it (remember, Navigation2D just uses Unity's built in navigation system)
     
  9. Kitobuddy

    Kitobuddy

    Joined:
    Mar 1, 2020
    Posts:
    3
    Hey, I just purchased Navigation2D, I am trying to learn how to use it for my game. I used Tilemap palette to create my areas.

    I have separate rooms in same scene but not touching each other, the door teleports the player and the camera to each room.

    I would like to have enemies follow the player to each room, by using the doors.

    Is there a way I can have the enemies recognize that the door is a pathway too the player? I added colliders to the layer that handles all the walls. It uses tile colliders, I can add a different one if needed, but I can not get the navmesh to show itself.


    Any help is appreciated.
     
  10. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,327
    Not 100% sure what you mean, but you can use agent.Warp to teleport agents
     
  11. Kitobuddy

    Kitobuddy

    Joined:
    Mar 1, 2020
    Posts:
    3
    Wo, what do you mean? Does it teleport to the same room? I just need it to be able to teleport into the room, from there it can follow the player like normal
     
  12. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,327
    Use NavMeshAgent2D.Warp :)
     
  13. Kitobuddy

    Kitobuddy

    Joined:
    Mar 1, 2020
    Posts:
    3
  14. Shpinxis

    Shpinxis

    Joined:
    Dec 4, 2014
    Posts:
    6
    Was wanting to use this with Polygon Colliders, but after reading the forum, it seems that it struggles with complex vertices that come this. If my meshes aren't generating correctly should I assume that it's the limitations of its use with Polygon Colliders and it needs simpler collides? Thank you.
     
  15. razorly

    razorly

    Joined:
    Sep 13, 2019
    Posts:
    1
    Replace method AddTilemapCollider2Ds with this

    void AddTilemapCollider2Ds(Transform parent)
    {
    // find all grids
    Grid[] grids = UnityEngine.GameObject.FindObjectsOfType<Grid>();
    foreach (Grid grid in grids)
    {
    // find tilemaps (we only care about those that have colliders)
    var tilemaps = grid.GetComponentsInChildren<Tilemap>().Where(
    tm => tm.GetComponent<TilemapCollider2D>()
    ).ToList();

    foreach (Tilemap tilemap in tilemaps)
    {
    // go through each cell
    BoundsInt bounds = tilemap.cellBounds;
    for (int y = bounds.position.y; y < bounds.size.y; ++y)
    {
    for (int x = bounds.position.x; x < bounds.size.x; ++x)
    {
    // find out if it has a collider
    Vector3Int cellPosition = new Vector3Int(x, y, 0);
    if (tilemap.GetColliderType(cellPosition) != Tile.ColliderType.None)
    {
    // convert to world space
    Vector3 worldPosition = tilemap.GetCellCenterWorld(cellPosition);

    // note: creating a primitive is necessary in order for it to bake properly
    var go = UnityEngine.GameObject.CreatePrimitive(PrimitiveType.Cube);
    var vertices = new List<Vector2>();
    tilemap.GetSprite(cellPosition).GetPhysicsShape(0, vertices);

    int[] indices = Triangulation.Triangulate(vertices).ToArray();

    // convert vector2 points to vector3 vertices
    var vertices1 = vertices.ToArray().Select(p => new Vector3(p.x, 0, p.y)).ToList();

    // create mesh
    var mesh = new Mesh();
    mesh.vertices = vertices1.ToArray();
    mesh.triangles = indices;
    //mesh.RecalculateNormals();
    mesh.RecalculateBounds();

    // assign it to the mesh filter
    go.GetComponent<MeshFilter>().sharedMesh = mesh;

    go.isStatic = true;
    go.transform.parent = parent;
    // position via offset and transformpoint
    go.transform.position = new Vector3(worldPosition.x, 0, worldPosition.y);
    // scale depending on scale * collider size (circle=radius/box=size/...)
    go.transform.localScale = NavMeshUtils2D.ScaleTo3D(tilemap.transform.localScale);
    // rotation
    go.transform.rotation = Quaternion.Euler(NavMeshUtils2D.RotationTo3D(tilemap.transform.eulerAngles));

    MakeUnwalkable(go);
    }
    }
    }
    }
    }
    }


    I had a similar problem and this mod resolved it for me.
     
  16. BigGameCompany

    BigGameCompany

    Joined:
    Sep 29, 2016
    Posts:
    107
    Hi,

    Purchased your asset recently at it works great. However, I opened my project today and even though the NavMeshAgent is working I can no longer see the blue NavMesh is the scene. Gizmos is on.....any ideas on why its no longer visible?
     
  17. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,327
    If it worked before then please go back through your backups to see which of your changes caused it
     
  18. ArtemiyChernyaev

    ArtemiyChernyaev

    Joined:
    Apr 29, 2020
    Posts:
    1
    Hi,

    I have a problem with a NavMeshObstacle2D. I want make NPC that follow player, but if didn't use Obdtacle, NPC bump into each other and teleport. This hapend when they haunted by a group. I start use NavMeshObstacle on NPC, but when I pointed out Carve boolean to true, I have error: "stopped agent because of collision in 2D plane". On NPC I have Box2D collider, RigBody2D.

    And when I choose Quality in Obstancle Avoidance my NPC start teleport to rand postions.
    What misstakes i make?

    Thank you in advance!
     
    Last edited: Jul 24, 2020
  19. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,327
    Hey,
    the main issue with NavMeshObstacle2D is that it uses Unity's built in NavMeshObstacle.
    Don't know about you, but personally I never figured out a good way to use them properly. It always looked extremely strange.
     
  20. StormSpirta

    StormSpirta

    Joined:
    Aug 19, 2019
    Posts:
    1
    Hi!
    Is it possible to bake 2D navmesh in real time?
    I need to bake it once after generate my map
     
  21. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,327
    It's not, sorry.
    Only works for 3D at the moment, because Unity's new NavMesh components with runtime backing are only for 3D.
     
  22. SmoothyD

    SmoothyD

    Joined:
    Sep 29, 2014
    Posts:
    8
    Hey,

    I copy pasted the code from Navigation2D to bake the navmesh into another script (well, only the tilemaps part) and ran it during runtime, it generates the navmesh fine.

    Why not make this directly possible with the plugin ? Is it because the generation process is too heavy for runtime ?
     
  23. arolian

    arolian

    Joined:
    Aug 20, 2016
    Posts:
    2
    is there a way to bake while running, making a random map generation
     
  24. arolian

    arolian

    Joined:
    Aug 20, 2016
    Posts:
    2
    saw the awnser above
     
  25. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,327
    V1.30 pending review:
    • Upgraded to Unity 2018.4 LTS
    • fix: baked 3D Navmesh is now positioned at y=0 and 3D objects are positioned at y=0.5 so that their feet are positioned at y=0 on the NavMesh. fixes a bug where NavMesh2D.SamplePosition would require a minimum distance of 0.5 to make up for the distance between the projected y=0 and the baked navmesh y=-0.5 before.
    Navigation2D is very stable now.
    First fix in over a year of using Navigation2D in uMMORPG2D :)
     
  26. jackblakeston93

    jackblakeston93

    Joined:
    Mar 19, 2021
    Posts:
    1
    Hi,

    I'm trying this with isometric tiles and my character gets stuck a lot, any advice?
     
  27. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,327
    Can't see your screen from here :)
    You can make a video. Although navigation2d was never meant for isometric
     
  28. Observer189

    Observer189

    Joined:
    Jun 11, 2019
    Posts:
    2
    Hello, Vis. I use Navigation2d with tilemaps. I have one tilemap for walls and another for ground. Baking works fine but it uses collider of all tilemaps. And I want to ignore ground. So the question: "Is there a way to make Nav2D to ignore colliders of some layer or some gameobject?"
     
  29. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,327
    Not at the moment, sorry.
    The ground shouldn't have a collider in a 2D game.
     
  30. Observer189

    Observer189

    Joined:
    Jun 11, 2019
    Posts:
    2
    It's important in my game that ground have a collider. And I easy resolved this problem by changing AddTilemapCollider2Ds method. Now this method check if the tilemap is navigation static. So I made the ground non-navigation static and that's it)
    But now I have an another question. Can I make an agents of different sizes? I mean an ogre mustnt pass through the small door but hobbit must.
    I search for this problem in google and I found that people uses NavMeshLayers for this. But looks like now this is obsolete. Now there are NavMeshSurfaces for this.
    But I have no idea how to use them in 2d. So maybe there is an another solution?
     
    Last edited: Apr 29, 2021
  31. renanssba

    renanssba

    Joined:
    Feb 22, 2014
    Posts:
    7
    Hello vis2k. I just bought the plugin and want to know if it's possible to rebake the navmesh during runtime.
    I just hit the "Bake" button during runtime after my random map is generated and the mesh updates just fine.

    Any plans to add the possibility to rebake the mesh during runtime? Many people asking for this, like SmoothyD and arolian above.
     
  32. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,327
    Not at the moment, sorry. Too much on my plate with Mirror and DOTSNET.
     
  33. renanssba

    renanssba

    Joined:
    Feb 22, 2014
    Posts:
    7
    Hello vis2k, after trying to mod it myself I've found no success in allowing it to rebake during runtime. My game is heavily reliant on the player customizing the scene and as it's not possible to rebake the navmesh during runtime, the asset does not suit my most important use case. Would it be possible to get a refund on this asset?
     
  34. dlorddd

    dlorddd

    Joined:
    Aug 13, 2016
    Posts:
    7
    Hey @vis2k
    we had extreme performance issues in a larger scene when trying to visualize the navmesh in scene view. I just added the following code block to the start of the OnGizmo in Navigation2D.cs:

    Code (CSharp):
    1. if (DateTime.Now.Ticks - lastDrawTime < 10000 * 16) return;
    2. lastDrawTime = DateTime.Now.Ticks;
    Just wanted to leave this here if anyone ever has similar issues.
     
    mischa2k likes this.
  35. hrohibil

    hrohibil

    Joined:
    Apr 17, 2021
    Posts:
    267
    Hello
    Pre sale question.
    1:
    Are there options for enemies to avoid each other?
    2:
    How does it bake the entire tile map ? I have different tile maps setup to have a large area.
    3:
    Any chances you would implement supporting scripts like patrol, chase, etc...?

    thank you..

    I am really interesting in question 1 how to proper setup avoidance
     
  36. chanfui

    chanfui

    Joined:
    May 2, 2017
    Posts:
    11
    Hi can I use it on indoor 2D navigation map, showing line renderer from point A to B, just like the Unity generic NavMesh components?
     
  37. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,327
    V1.31 released!
    - added lots of unit tests to guarantee stability
    - fix: PolygonCollider creation now uses Unity's built in Collider2D.CreateMesh function instead of custom Triangulation algorithm.
    Fixes https://github.com/vis2k/uMMORPG2D/issues/15
    - fix: NavMesh2D.SamplePosition now automatically adds the distance between the 3D navmesh and the 3D ground so that passing distance=0 for a 2D point on Navmesh works too
     
  38. Griffon2_3

    Griffon2_3

    Joined:
    May 6, 2021
    Posts:
    3
    Hello, is it possible to update the baked navmesh at runtime? If so, how to do it?
     
  39. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,327
    not supported because it uses unity's built in navmesh.
    you'd have to modify unity's 'navmesh components' on github, which support runtime baking.
     
  40. Deleted User

    Deleted User

    Guest

    Hi guys, I implemented a gathering system for resources in my 2D game. Those resources disappear after being gathered. Unfortunately their collider is still active, because Navigation2D need to be baked again. Is there any way to bake dynamically during runtime or is there any other options to solve this? Thanks in advance
     
    Last edited by a moderator: Aug 14, 2022