Search Unity

Bug NavMesh agent speed slows down the higher the framerate

Discussion in 'Navigation' started by Jarko89, Oct 22, 2020.

  1. Jarko89

    Jarko89

    Joined:
    Mar 8, 2017
    Posts:
    15
    Hi all,

    I'm having an issue that's basically already explained in the title. I have a player GameObject with a NavMeshAgent component, speed is fixed and set in the component. I calculate a path with agent.CalculatePath() and make the GO move with agent.SetPath().

    The issue I'm facing (on built players) is that when the framerate is uncapped, the speed of the GO is visibly slower. There's not difference between 30 and 60 FPS, but it's visible with higher values. If the game is running at 120 FPS the agent is likely 20-30% slower (even though the agent.speed parameter is always the same).

    I've tried with different unity versions (currently 2019.4.9f1) and the issue is always the same. I couldn't find anything on the forums on the matter.

    What's going on there and how can it be prevented?

    Thank you!

    EDIT: The NavMesh is dynamically generated around the player, close to how it's done here. The issue is the same with a pre-baked NavMesh though.
     
    Last edited: Oct 29, 2020
  2. seregin-pro

    seregin-pro

    Joined:
    Aug 14, 2020
    Posts:
    1
    1. Open AI - Navigation
    2. Tab Bake. Set agent radius 0.4 or higher.
    3. Press Bake button

    It is saved me.
     
  3. AlienKatGames

    AlienKatGames

    Joined:
    Feb 14, 2020
    Posts:
    2
    Are you multiplying speed with Time.deltatime? I was having the same issue, but when I removed Time.deltatime the speed fixed itself.
    before:
    agent.speed = speed * Time.deltatime;
    after:
    agent.speed = speed;
     
  4. Jarko89

    Jarko89

    Joined:
    Mar 8, 2017
    Posts:
    15
    Thank you but I don't get how that would apply to our problem? The NavMesh is dynamically generated around the character by the way, close to how it's done here. Adding this bit of info to the question :)

    Nope, I specified the speed is fixed and set in the component :)
     
    Last edited: Oct 29, 2020
  5. michaelday008

    michaelday008

    Joined:
    Mar 29, 2019
    Posts:
    135
    I am experiencing this issue myself. as @Jarko89 noted, the speed is fixed and set in the component. I have a cutscene where some units run toward an object and cast a spell on it. As I improved the speed of my project, I started to notice that one or more of the units didn't make it to the object in time, and were still moving when the cutscene sent them the signal to perform the spell cast.

    When I started writing some debug messages to try to figure out what was going on (which would decrease the frame rate from 120fps to ~90 fps), suddenly the units were moving a lot faster, and would always reach the target a full 2 seconds faster.

    When I comment out the debug messages, the frame rate goes back up and they are slow again. While performing this testing, I have Obstacle avoidance quality set to None, and Angular Speed and Acceleration set to 100,000 so it's nothing to do with the NavMesh Agent setttings. The issue is entirely frame rate dependent.

    I have a feeling there are some internal calculations happening inside the actual NavMeshAgent code that are using Time.deltatime, because there is nothing in my code that references it.

    [EDIT]
    I did some further testing and added so many debug messages per frame (and video capture of the cutscene) that I got frame rates down to ~30fps. The NavMesh agent reached the position in ~13 seconds. Compared to ~17 seconds at 120 fps. That means the NavMeshAgent calculations are so bad that you see a 20% increase in agent speed when you drop from 120 fps down to 30 fps.

    This is a big deal (in a bad way) because the faster the machine of the person playing this cutscene, the worse it will look due to the agents not reaching key points in time.
     
    Last edited: Aug 26, 2021
  6. loaikk9

    loaikk9

    Joined:
    Jan 13, 2021
    Posts:
    3
    Did anyone solve this issue yet?
     
  7. David_Faulkner

    David_Faulkner

    Joined:
    Oct 5, 2013
    Posts:
    33
    I might have just run into something similar.

    I noticed some odd speed issues when moving a character around with a navmesh agent. After pulling my hair out all afternoon, I've figured out it was being caused by having the interpolation setting enabled on the rigidbody component I have on the same object.

    Sometimes I disable the navmesh agent and drive the character movement with physics, so I enabled interpolation to prevent jittering issues with the following camera. It seems having the interpolation enabled at the same time as the agent causes this weird slow down issue. Now I only enable it when I'm using the physics and it seems to be fixed.

    Not sure if this is the same issue, but I hope this helps someone.
     
    aventu, Brut_dev and swampfriend like this.
  8. Brut_dev

    Brut_dev

    Joined:
    Aug 10, 2019
    Posts:
    2
    Thank you. Ran into the exact same issue, with the exact same reason, and this solved it.
     
    David_Faulkner likes this.
  9. DeLong

    DeLong

    Joined:
    Feb 22, 2009
    Posts:
    37
    Thank You - David! I've been wrestling with navmeshes for a week. Searched the internet exhaustively for solutions. Had things up and running pretty quick in editor initially. Agents were smooth in the editor, then I checked the build after having it all set up and they're all jittery and slow. Man I was becoming very disenchanted with Unity navemeshes up till now. If you don't have your agents and code order setup just right for custom complex agents, you're in for a ride. I have a dragon that breaths fire when it reaches it's destination. He was the biggest problem trying to stop the agent completely, play breath fire animation, then resume after finished breathing fire. Caused some timing issues, but eventually figured it out. Animation events helped solve that problem. Also had some race conditions that only popped up in the build with the dragon only. I'd fix one problem and create two more. But setting the rigidbody interpolation to none solved my final issue of slow, jittery agents in the build only problem. Time to do the happy dance!
     
    David_Faulkner likes this.