Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  3. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Divorce NavMeshAgent work from main Update() loop

Discussion in 'AI & Navigation Previews' started by Claytonious, Dec 5, 2017.

  1. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    904
    NavMeshAgent currently does its work automatically every frame and there isn't any API exposed to manually "Tick" instances of NavMeshAgent. We would like to, for example, have a game that runs at 60fps, but have NavMeshAgents only do their "thinking" at something like 20fps - so we would like to call Update() on those agents every 3rd frame in this example (probably with a distribution of work across all instances - those details don't matter).

    We already have updatePosition/rotation = false and are handling the details of movement ourselves. But we have a lot of our CPU time being eaten by NavMeshAgent AI right now. We want some control over this.

    This is analogous to the work that's recently been done in the Physics system to allow us to programatically run physics simulation frames.

    Is there any way to do this currently? Maybe something that we could call via Reflection? Are there any plans to expose this in a supported way?
     
  2. JBR-games

    JBR-games

    Joined:
    Sep 26, 2012
    Posts:
    708
    From my experience a good boost to fps is to make sure you dont use agent.setpostion() every frame.. do this on a couroute or invoke function that is called 2-10 times per second.. Most of the overhead seems to be during the one frame this is call.
     
    Claytonious likes this.
  3. JBR-games

    JBR-games

    Joined:
    Sep 26, 2012
    Posts:
    708
    http://forum.unity3d.com/posts/2457252/

    This link will allow you to use the navmesh but control an object as you choose, this will get you part of the way. 100's - 1000's of moving objects on navmesh...
     
  4. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    904
    That link is about culling skinned mesh renderers, right? I'm not having any problems with graphics performance. I'm struggling to get NavMeshAgent to use less CPU.
     
  5. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    904
    Interesting - thanks for that tip. We are only setting NavMeshAgent.nextPosition, and pretty rarely (only when it reaches a certain distance away from the "actual" transform.position of the object), but still ... will have a look.
     
  6. JBR-games

    JBR-games

    Joined:
    Sep 26, 2012
    Posts:
    708
    No have a look at rusfighters code in that link. It does a navagation check away from the agent.. Allows many object to use navagation at once.with little overhead.

    Later on in the link i add code for further optimizations like renderers. And culling groups..
     
  7. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    904
    Understood. I've been down that road of just using pathfinding on its own and navigating the corners array manually. That's a good solution for some scenarios, but this time I need the local avoidance and crowd management that nav mesh agents perform outside of simple path following.

    We need a way to explicitly "tick" agents instead of them being hardwired to do work on every frame.
     
  8. JBR-games

    JBR-games

    Joined:
    Sep 26, 2012
    Posts:
    708
    Yes it would be nice if Unity opened up the navmesh API to allow us to adjust things like that. For that matter it might be possible that they did already as a lot of the stuff they go open source with on things like gethub but they don't document it or it's hard to find out about it.
     
  9. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    904
    JBR-games likes this.
  10. HouseOfHowGamesLLC

    HouseOfHowGamesLLC

    Joined:
    Jan 15, 2015
    Posts:
    5
    We could really use this as well, for a different reason- We're making a turnbased game and want to simulate everything on the server as fast as can be calculated (so we manually tick physics), record the results, and send it to the clients.

    We had been using PolyNav before, which we could manually tick (with a slight source change) and it worked great, but decided to switch into 3d and so are back to trying to get it to work with NavMeshAgent =/.
     
    Claytonious likes this.