Search code examples
c#unity-game-engineprocedural-generationnavmesh

Suggestion on NavMesh 2D generation at runtime with infinite procedural generation


Let me start off by saying I began my jounrey of programming within the last year and Unity about the same time. I have come a long way since then, but I still come across walls that I cant seem to get across, this is one of them.

I'm making a 2D game where one of the modes is to dungeon crawl, infinitely, with parts of the level being chosen from prefabs (mostly just premade chunks of a level that get stitched together when generating) on a list randomly when the player reaches a certain distance from the end of the last instantiated prefab. I got this part down, I can create prefab, after prefab, after prefab and everything works nicely! But, I'm having trouble adding a NavMesh to these newly instantiated prefab chunks so that my player stays within bounds. I am using the NavMesh components git to make NavMesh2D possible.

I have tried:

  1. making the prefab itself with a predefined baked NavMesh, but the player can only go as far as the first chunk, and stops, because he reached the end of the current NavMesh, even though the next NavMesh is overlapping/right next to, 1 unit away (all variations tried).
  2. Bridging these NavMesh with links and off mesh links, doesn't work.
  3. Dynamically creating a NavMesh at runtime around the player as he progresses, doesn't work.

I am honestly stuck at this point and don't know what to try. I am open to suggestions on how to proceed at this point and welcome all input. If you need any info just ask! Or code, just ask! I'm not sure what to attach, so just ask away!

Also, if there are other pathfinding options I am open minded about them as well. Hope to hear from anyone soon! Thanks!


Solution

  • I have found this thread that talks about NavMesh baking at runtime. How to "bake" NavMesh from script at runtime?

    In the top answer there are links for some of Brackeys tutorial i was also about to reccomend if you havent checked them already and also links to Unity's manual about baking NavMeshes at runtime.

    I think those might help you out.

    When thinking this from my point of view, and from what i understand from your project, you need to generate new prefabs when your player is close to the new not yet generated area. When you do that, you can bake a new NavMeshSurface after creating them. When the player reaches close to the end of that area, repeat the process.

    You should have your prefabs NavMesh settings ready, and the NavMesh should be built properly according to those settings.

    Hope that helps a bit.