- Home /
How to prevent Navmesh Agent from overshooting destination with high Time.TimeScale?
I've noticed that if I increase the Time.TimeScale, the agent will overshoot its destination for a few frames (I'm assuming this happens since the Update() function runs less times per frame and so it lacks precision once the timeScale is increased.
To try and fix this, I've turned the agent updatePosition to false and I'm manually doing the:
transform.position = agent.nextPosition;
So my question is, what is the correct procedure to make sure that the calculations for the next transform.position are accurate, even with high Time.timeScale?
Answer by castor · Dec 09, 2016 at 05:30 AM
Finally figured it out. In case anyone else has the same issue, this is what worked in my case...
First step is to disable navMeshAgent updatePosition like I mentioned above. Then I manually move the agent based on my timescale by doing the following:
if (Time.timeScale > 1.0f && agent.hasPath) {
NavMeshHit hit;
float maxAgentTravelDistance = Time.deltaTime * agent.speed;
//If at the end of path, stop agent.
if (
agent.SamplePathPosition(NavMesh.AllAreas, maxAgentTravelDistance, out hit) ||
agent.remainingDistance <= agent.stoppingDistance
) {
//Stop agent
}
//Else, move the actor and manually update the agent pos
else {
transform.position = hit.position;
agent.nextPosition = transform.position;
}
}
Your answer
Follow this Question
Related Questions
Precise distance calculation with high timeScale for NavMeshAgents? 1 Answer
NavMeshAgents with same variables acting differently 1 Answer
Change AgentType at runtime. 0 Answers
Root Motion and Navmesh Agent 0 Answers
Is it possible to store NavMeshAgents paths and assign them to other NavMeshAgents later? 1 Answer