Update the navmesh after carving
Hi there, I'm trying to have my characters act as Nav Mesh Agents when they're moving but as Obstacles (with carving) when they're standing still so that other characters go around them instead of pushing them aside. Each character has both the Agent and Obstacle component on in and it just enables/disables the one that's required. When I set a destination for the Agent, I also spawn an empty gameObject at the destination point that has a Collider. When the character Collider hits the destination one, it switches from the Agent to the Obstacle as it knows it no longer needs to move.
The issue I'm having though is that when I set a new destination for the Agent, so basically I turn the Obstacle off, turn the Agent on and tell it its new destination, instead of moving from its current location it sort of jitters a little bit to a new position close nearby and goes from there instead. That's because the Nav Mesh updates itself briefly. Previously, it was being carved by the Obstacle component and now it no longer does that since the Obstacle is diabled.
If I add a 0.25 second delay between disabling the Obstacle and enabling the Agent all works well, but I assume that's not the proper way to do it. I've been searching online for similar posts, however I can't seem to figure out what the code syntax is to either manually call the nav mesh update so I can place it between the components switch or to add a call-back for when the nav-mesh finished updating so I can enable the agent then.
Can anyone please help? Thanks!
Answer by Harinezumi · Feb 06, 2018 at 12:46 PM
I don't have a definitive answer, only ideas, but some may help...
Maybe checking if the path is valid and only starting the agent then (setting velocity to > 0), would work? You can get the path either directly from the NavmeshAgent, or through CalculatePath(), and check if the path is complete.
You could also wait for NavmeshAgent.isOnNavMesh to become true.
Finally, maybe Unity's Navmesh components can help with this?
I really like the path being valid or navmesh agent being on navmesh ideas, I'll try them out, thank you!
Hey Harinezumi, I have tried your suggestions and the issue is that if the Nav$$anonymous$$eshAgent is enabled while it's not sitting on the Nav$$anonymous$$esh, it gets teleported to the closest possible area on the Nav$$anonymous$$esh. So unfortunately, getting a path or checking if the Nav$$anonymous$$eshAgent is on a mesh can't work. The Nav$$anonymous$$eshAgent can only be enabled after the Nav$$anonymous$$esh has been updated and the carving from the obstacle has already been removed.
So the conclusion is that I still need to figure out somehow when the Nav$$anonymous$$esh has been updated or call the update manually. I have found the following: https://docs.unity3d.com/ScriptReference/AI.Nav$$anonymous$$eshBuilder.UpdateNav$$anonymous$$eshData.html but I can't really make sense or figure out how to write it. I have public Nav$$anonymous$$eshSurface $$anonymous$$yNav$$anonymous$$eshSurface; declared and assigned through the inspector. So I can get the data via $$anonymous$$yNav$$anonymous$$eshSurface.nav$$anonymous$$eshData. I can also get the build settings via $$anonymous$$yNav$$anonymous$$eshSurface.GetBuildSettings and bounds through $$anonymous$$yNav$$anonymous$$eshData.sourceBounds but I have no idea how to add $$anonymous$$yNav$$anonymous$$eshSurface as one of the sources :(
Interesting! This is a new API since Unity 5.6, and it wasn't even announced as a feature!
Reading the (worse than usual) scripting manual for it, it seems the key is giving UpdateNav$$anonymous$$eshData() a list of Nav$$anonymous$$eshBuildSources, which can be automatically created using Nav$$anonymous$$eshBuilder.CollectSources().
However, I haven't tested this, and the API seems very experimental (there is basically no more information about how it works, what it does), so I don't know how well it works, what are the limitations, etc.
Your answer
Follow this Question
Related Questions
NavMeshAgent freeze for a moment 0 Answers
I have patches on nav mesh 0 Answers
Navmesh Question A way to rebake Navmesh after destroying obstacle 1 Answer