h8man / NavMeshPlus

Unity NavMesh 2D Pathfinding
MIT License
1.73k stars 200 forks source link

Runtime mesh baking #155

Closed AndreasLrx closed 1 year ago

AndreasLrx commented 1 year ago

I am trying to bake my mesh at runtime because I am making procedural room generation and therefore can't bake the nav mesh in the editor.

I tried to simply

NavMeshSurface navMeshSurface = Instantiate(navMeshSurfacePrefab);
navMeshSurface.BuildNavMesh();

However I got a debug log telling me I have 0 sources. My NavMeshModifier.activeModifiers.Count is not equal to 0 (~300).

I'm reading the scripts since two hours and still can't find anything to help me, and any tutorials I see are on editor and not on runtime. I'm not familiar with unity navigation mesh so excuse me if I'm making beginner mistakes.

Whatever the issue is, I think it would be great to add a section in the readme/doc to explain how to generate the mesh at runtime (in case it's not possible at editor time)

h8man commented 1 year ago

Here is demo for you https://github.com/h8man/RedHotSweetPepper including runtime

AndreasLrx commented 1 year ago

Thank you I haven't seen this.

All I was missing was to add a RootSource.

NavMeshSurface navMeshInstance = Instantiate(navMeshSurface);
navMeshInstance.GetComponent<RootSources2d>().RooySources.Add(boardHolder.gameObject);
navMeshInstance.BuildNavMesh();

However as you can see, there is a typo in the RootSources2d script: RooySources instead of RootSources https://github.com/h8man/NavMeshPlus/blob/master/NavMeshComponents/Scripts/RootSources2d.cs#L19

h8man commented 1 year ago

fixed