Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

NavMeshAgent Move in FixedUpdate

Discussion in 'Navigation' started by emrys90, Apr 28, 2017.

  1. emrys90

    emrys90

    Joined:
    Oct 14, 2013
    Posts:
    489
    Is there a way to tell an agent to apply movement in FixedUpdate instead of Update? Without having to write our own movement code that is.
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    25,844
    Perhaps it's better to explain why since it does need code.
     
  3. emrys90

    emrys90

    Joined:
    Oct 14, 2013
    Posts:
    489
    Well I mean you set the position that the agent moves to, and it applies its own movement logic in Update it seems like. I'd like to happen in FixedUpdate instead. The reasoning is it's a networked game, and movement it synced to the clients in FixedUpdate. Any objects that move in Update often appear jittery on the clients. I use Bolt for reference.
     
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    25,844
  5. emrys90

    emrys90

    Joined:
    Oct 14, 2013
    Posts:
    489
    I was more hoping that Unity would consider adding in a setting for when it gets updated, like the Animator component has. I can't be the only person this would benefit to have a toggle for this, it seems like anyone that uses AI in a networked game this would be a necessity.
     
    SM_AF and Multithreaded_Games like this.
  6. Multithreaded_Games

    Multithreaded_Games

    Joined:
    Jul 6, 2015
    Posts:
    121
    You indeed would not be the only person to benefit from this. It honestly seems like it shouldn't be that difficult to add and should work similarly to the Animator component allowing the user to toggle the update type.
     
    SM_AF likes this.
  7. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    25,844
    You can turn off automatic movement and get an intended velocity from the agent, which when added to transform.position, moves it identically. I believe this is precisely what you are both asking for.

    If you are waiting for this to be added you should do it yourselves ASAP as I have no idea if Unity staff is even reading this, let alone intending to ever add it. Just my 2p.
     
  8. Multithreaded_Games

    Multithreaded_Games

    Joined:
    Jul 6, 2015
    Posts:
    121
    I actually don't want to use the NavMeshAgent to drive the character at all--I simply want to keep the physics-controlled (FixedUpdate) non-kinematic character 'restricted' to the NavMesh in a similar manner to how he would be constrained if he were a kinematic object being moved in Update. The problem is that these two update modes are inherently incompatible. You can have nice, smooth NavMesh movement, but you can't have smooth collisions or vice-versa. The reason is because the NavMesh reposition happens in Update, whereas velocity-driven movement happens in FixedUpdate and are basically arguing with each other. Don't even get me started on how a camera fits into this...

    It's not a typical use-case, I understand, but it literally seems impossible to get the behavior I want. Our character will always be grounded and we don't need a full-blown physics simulation when handling collisions with static objects since they would be baked into the NavMesh. Futhermore, level design is far easier when you can literally see every accessible location with the NavMesh. Otherwise, if we go the full physics route, we'll be hunting for gaps and collisions for god knows how long with the amount of content we already have.

    I can almost use both of these things by moving the character in FixedUpdate and then 'correcting' the movement with the NavMesh's sampled position, but there are strange quirks with NavMesh's SamplePosition function that causes the character to occasionally get stuck on NavMesh seams (which I actually posted about in another topic.) Since there are no clear answers on how Unity is internally 'keeping' the character confined to the NavMesh, I'm pretty much stuck on this.

    Further adding to the frustration is that there are a few posts talking about SamplePosition returning incorrect results or points that aren't actually on the NavMesh. You would think that Unity is using this same function, no?
     
  9. Jakob_Unity

    Jakob_Unity

    Unity Technologies

    Joined:
    Dec 25, 2011
    Posts:
    269
    There's not a way to make the agent move in fixed update w/o having to write some code - no.
    We're working on the runtime query API at the moment - and it will be async and update/tick agnostic.
     
    SM_AF likes this.
  10. Gooren

    Gooren

    Joined:
    Nov 20, 2015
    Posts:
    138
    Sorry to resurrect this old thread, but did this magical runtime API come to fruition yet?
     
    hippocoder likes this.
  11. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    161
    Last edited: Nov 11, 2019
    Gooren likes this.