Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Manual "Update" for NavMeshAgents?

Discussion in 'Navigation' started by Claytonious, Aug 6, 2019.

  1. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    900
    One of our recurring challenges with using NavMeshAgent is that it is tightly bound to the Unity frame lifecycle: it automatically updates every frame. We would like to be able to do things like manually update an agent only when we wish instead of every frame. For example, we might have a simulation where the "view" renders at 60fps but we only want to run the "simulation" at 10fps (and maybe interpolate the visuals in between). Or we might be doing turn-based games where turns are "simulated" outside of the main render loop, etc.

    Is there any hope of doing with NavMeshAgent something similar to what was done with the Physics engine, which is to turn off automatic updating every frame and instead allow us to explicitly update a single "frame" from script whenever our own logic dictates?

    Thanks!
     
    smritichopra3 and Peter77 like this.
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    I think this is possible yes. If I'm understanding things right, you first set a path and immediately stop the agent using agent.SetPath and agent.isStopped = true.
    If you want to interpolate your movement you might also have to set updatePosition and updateRotation to false.
    Then, you can call agent.Move to move your simulated position with an offset from your current position.
     
  3. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    900
    I should have mentioned this up front in the question but yes, that's what you have to do today to turn off NavMeshAgent's default behavior, but you completely lose the agent's built-in local avoidance and such niceties. By doing this, you are taking over total responsibility for the agent's movement and losing all of its built-in movement.

    We've done exactly that, combined with our own calls to CalculatePath and our own logic to move along the computed paths.

    I'm asking if Unity might consider implementing a way to keep all of the agent's built-in movement/behavior without updating every frame.
     
  4. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    In theory it should work.. Detour actually takes full control of movement, it has an update method and takes a delta time. But who knows how much they have customized or added to it where it's not just a simple thing.
     
  5. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Hmmm, is the avoidance logic not implemented with steeringTarget? If not, then I'm afraid you indeed don't have access to the underlying systems.
    Unity's navigation system is unfortunately pretty blackbox-y.
     
  6. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Another option is use the job based api's. Those will get you what you want with some caveats.

    A potentially big caveat is crowd handling you would need to implement yourself.

    The other thing is the job based api is basically exposing lower level recastnavigation api's. Great if you want the control but understand there is a whole lot you have to now do yourself that Unity's higher level api's handle for you. The Austin Unite demo has most of the pieces you need and is a good guide for how to put it all together. But it's not complete there are bits you will need to sort out on your own.

    For instance you have to handle all the failure/retry logic yourself based on low level query results. You have to straighten the path and then navigate along that using lower level api's. It's a chunk of work. Plan on a couple of weeks full time for a good developer to get all the finer details worked out.