News Forums RAIN General Discussion and Troubleshooting AI Navigators ignore NavMesh generated from code

This topic contains 7 replies, has 2 voices, and was last updated by  7BiTStudios 1 month, 2 weeks ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #39633

    7BiTStudios
    Participant

    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)
    NavMesh
    Behaviour

    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.
    #39638

    Sigil
    Keymaster

    Well, 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?

    #39639

    7BiTStudios
    Participant

    Sadly 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.

    #39641

    7BiTStudios
    Participant

    I just tested: New agents correctly use the updated NewMesh.

    #39652

    7BiTStudios
    Participant

    Sigil, 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!

    #39655

    Sigil
    Keymaster

    I 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.

    #39660

    Sigil
    Keymaster

    OK, 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.

    #39665

    7BiTStudios
    Participant

    Thank 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!

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.