News › Forums › RAIN › General Discussion and Troubleshooting › AI Navigators ignore NavMesh generated from code
Tagged: AI, navigator, navMesh, regenerate, Update
This topic contains 7 replies, has 2 voices, and was last updated by 7BiTStudios 1 month, 2 weeks ago.
-
AuthorPosts
-
December 8, 2022 at 8:49 am #39633
Hello!
My project requires me to regenerate my NavMesh during runtime.
The actual NavMesh generation is done using this code:/// <summary> /// Starts a new Coroutine to rebuild the nav mesh assigned to this village. /// </summary> public void RebuildNavMesh() { StartCoroutine(RebuildNavMeshCoroutine()); } private IEnumerator RebuildNavMeshCoroutine() { LogStatus("Start NavMesh Generation", this); _navMesh.NavMesh.UnregisterNavigationGraph(); _navMesh.NavMesh.StartCreatingContours(4); while (_navMesh.NavMesh.Creating) { _navMesh.NavMesh.CreateContours(); yield return new WaitForSeconds(1); } _navMesh.NavMesh.RegisterNavigationGraph(); _navMesh.Awake(); LogStatus("End NavMesh Generation", this); Notifications.Post(this, Strings.Notifications.NAV_MESH_REBUILT); }
However, my dummy agent who is following a Waypoint Route ignores the new NavMesh and keeps following his old path despite the updated NavMesh showing up visually in the SceneView. (The house below was placed during runtime from a script)
I tried these methods in the hope that they’d update the Navigators behaviour, but unfortunately it did not work.
private void Awake() { Notifications.Register(gameObject, Strings.Notifications.NAV_MESH_REBUILT); } private void OnNavMeshRebuilt() // Raised as soon as the NavMesh has been rebuilt. { LogStatus("Nav Mesh was rebuilt, updating navigator (?) somehow (?)", this); AiRig.AI.Navigator.RestartPathfindingSearch(); // "AiRig" is a property assigned to this behaviour. AiRig.AI.Navigator.AIInit(); }
- This topic was modified 1 month, 2 weeks ago by 7BiTStudios.
- This topic was modified 1 month, 2 weeks ago by 7BiTStudios.
December 8, 2022 at 11:19 am #39638Well, what you are doing looks correct, I’ll put together a sample on my end to see if I can recreate the issue though.
Does your dummy agent ever realize he is on a new navigation mesh? Like maybe once it gets to the next waypoint it starts following the navigation mesh then, or does it never find the new navigation mesh?
December 8, 2022 at 11:35 am #39639Sadly not. I watched him go through the entire Waypoint Route 5 times, and he keeps on following the old NavMesh. So it is as if he has the old navigation mesh cached and just keeps on using that.
December 8, 2022 at 11:57 am #39641I just tested: New agents correctly use the updated NewMesh.
December 9, 2022 at 1:50 pm #39652Sigil, were you able to reproduce the problem? I am still facing the problem and the only working solution I have found so far is to destroy and re-instantiate all my AI characters. Whilst it works for some very basic AI I am sure that this is something I cannot do anymore in the final game (for performance and complexity reasons).
Sorry to bump the post only after a day, but it is very difficult to write and test behaviour trees for AI that ignores my NavMeshes I really appreciate your time!December 9, 2022 at 10:27 pm #39655I am able to reproduce the problem, I’m looking into what is causing it now, if I figure it out today I’ll let you know, otherwise I’ll get back to it as soon as I can.
December 10, 2022 at 9:52 am #39660OK, so I will have a better fix in RAIN in the next version, but the simple workaround is to do this:
private void OnNavMeshRebuilt() // Raised as soon as the NavMesh has been rebuilt. { LogStatus("Nav Mesh was rebuilt, updating navigator (?) somehow (?)", this); AiRig.AI.Navigator.CurrentGraph = null; // "AiRig" is a property assigned to this behaviour. }
So the only time the AI ever double checks that the graph is correct is if it is wandering off graph (in case another navigation mesh shows up). Also, regenerating the graph essentially drops the previous graph and allows it to get garbage collected, instead of working within the same space. Both of those things aren’t ideal, so I will fix that.
In the meantime, your code will work with that one change, and in the next version of RAIN you won’t need to tell the AI to repath at all, it’ll just pick up the change.
December 10, 2022 at 10:55 am #39665Thank you very much! This works out perfectly as a temporary solution. Looking forward to the next update.
Thanks for taking your time and providing such an amazing toolkit to the community! -
AuthorPosts
You must be logged in to reply to this topic.