- Home /
When baking a Navmesh Surface at runtime 'Include Layers' property gets ignored
I am using the Unity NavMesh Components to bake a randomly generated map in my game.
I use the NavMesh Surface component to be able to bake during runtime. I have my player gameObject standing on top of the map ant to make it so that the area around it does not get cut out I set the player (and all it's child objects) on a separate layer called "Player". Then I set the Surface 'Include Layer' property to every layer except the "Player" one. When I bake in the scene (not during runtime) the player gets ignored and the map around it is set as walkable (highlighted in blue).
The problem is that without changing anything (no parameters, no layers, no properties…) when I bake during runtime the player is counted as an obstacle and the map around him is cut out of the NavMesh.
I bake the surface at runtime like this:
using UnityEngine;
using UnityEngine.AI;
public class NavigationManager : MonoBehaviour
{
public NavMeshSurface surface;
public void Bake()
{
surface.BuildNavMesh();
}
}
I think I could "fix" this issue by moving the player up in the sky before baking and then bringing it back to its position, but this is just a work around. Am I doing something wrong, or is it a bug of the NavMesh Components?
Answer by jihadkhawaja · Dec 21, 2019 at 12:32 AM
First of all please check if these are set correctly:
Navmesh agent component collider is placed correctly on player model.
Be sure you applied the Navmesh included layers on the level gameobject.
Check for any ignore from build Navmesh modifier component on any of the level gameobjects.
Be sure that the Navmesh is set to calculate Mesh instead of physical collider incase u didn’t add colliders to the map
Try to manually bake your Navmesh on runtime to find the issue.
Lastly provide screenshots for your Navmesh components in case this didn’t help.
Thanks @khawajajihad
Here are the supporting images https://imgur.com/a/ciORmS2
This is immediately after changing obstacle layouts and rebuilding the navmesh during runtime. You can see that I'm ignoring a few layers. I included an image of the Toe as an example of a collider on the Player object that is touching the surface that the navmesh is being built on. All GameObjects on the Player that include a collider are tagged with the $$anonymous$$yPlayer tag. There are no other colliders occupying this space during the rebuild of the navmesh.
You can see that the obstacles that I intend to build the navmesh around are properly included in the build and the navmesh is not generated in those areas. I am using the Physics Colliders geometry setting, which I believe is appropriate for my setup. I do not have a Nav$$anonymous$$eshAgent on the Player GameObject, but I don't think that should impact the navmesh that is generated.
Let me know if you have any ideas! Thanks
@$$anonymous$$eogh1413 can u please include all layers and then try to generate a map and bake it ay runtime to see if the error persists , if it was fixed that means you need to make a script which set the layer name of each gameobject instantiated on awake and the bake navmesh. However if the issue still persists then it might be that the box colliders (if ur not using mesh col) are not in the correct on of the generated map gameobjects. Hope this helps otherwise we need to do more debugging.
Were trying to annihilate possibilities here.
Got it!
So, when I set layers back to Everything, I noticed that the hole got bigger. That made me wonder which colliders were being included in the build. So, in Nav$$anonymous$$eshSurface after collecting sources (https://github.com/Unity-Technologies/Nav$$anonymous$$eshComponents/blob/master/Assets/Nav$$anonymous$$eshComponents/Scripts/Nav$$anonymous$$eshSurface.cs#L341) I printed out each source collected. This led me to find a rogue collider on a non-player object (the VR SD$$anonymous$$ head, to be exact) that was not in an ignored layer. Adding this rogue collider to an ignore layer fixed my issue.
I noticed also that even if an item is not colliding with the ground, it'll be included. Is it possible to ignore colliders that are not on the floor surface itself?
Thanks for the help!
Glad to help and see ur issue being fixed
Your answer
Follow this Question
Related Questions
How to know if a navmesh is baked at runtime? 0 Answers
Procedural NavMesh Instantiating 0 Answers
Nav Mesh only rebake changed areas? 0 Answers
Limit area nav mesh is baked on terrain 0 Answers