Controlling when the NavMeshAgent updates.

Hi all,

I am currently working on a unity project where I am creating a crowd evacuation simulation. I currently have a problem with creating a deterministic simulation because of the way that the NavMeshAgent updates on the AIUpdate which is part of the PlayerLoop.
From what I can tell (The docs are not super helpful with this), AIUpdate is called every frame.
This couples NavMeshAgents’ behaviour to the runtime frame rate.

This isn’t ideal for my case because I need my simulation to be deterministic, and also to be able to run independent of real time (i.e as fast as it can execute) for this I need fixed time steps that the agent is updated (e.g on the fixed update loop).

I have scoured the forums, docs, and discords for a solution to this , but have got nowhere.

I found this post where the op wants to use the FixedUpdate loop to update the NavMeshAgent, but with no solution was found.

One suggestion that keeps coming up is to disable the NavMeshAgent’s updating

    agent.updatePosition = false;
    agent.updateRotation = false;

And then applying movement in the Fixed Update loop using agent.desiredVelocity although I’m unclear exactly how to do this (update transform? warp? nextPosition?)

But this solution is unviable for me anyway because AIUpdate may be called several times every fixed update, which means the all my agents (potentially thousands) are needlessly planning their velocities for several update steps since the “simulated agent” is still being updated even if agent.updatePosition is false. (All updatePosition does is tell the “simulated agent” to update its transform)

Just to expand upon the details of my use case since it’s quite a unique one, my number one priority is determinism, then performance (only since I will have thousands of agents). Visual quality or consistency is not a concern. And I’m using NavMeshAgents both for the A* pathfinding and the the RVO based local avoidance.

If I can’t solve this issue then my next step is to implement navigation from scratch (A* + RVO or SFM or similar) but this will be quite an undertaking (maybe in DOTS?)

1 Like

AFAIK there is no way to make the current Unity pathfinding system deterministic. I would recommend looking into DOTS, though I am not sure if the determinism they have on their list of feature is already working.

Surely making it deterministic is as simple as allowing me to control when it’s updated. It looks like the source code for the NavMeshAgent open so I can’t implement this behaviour myself. My simulation simply needs to use fixed time steps that I have control over.
I’m curious why you think that this might not even be possible in DOTS?

There are ways to modify the PlayerLoop callbacks - I’ve found it to be very powerful. Check out this link for an Editor utility and example of how to do it: Player Loop Visualizer: Built to explore the new PlayerLoopSystem api in Unity 2018.1b2. This tool shows you all the PlayerLoop systems that unity uses to update a frame, and demos how to add your own and even remove systems from the player loop. For more info see the patreon post https://www.patreon.com/posts/unity-2018-1-16336053 · GitHub