Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question NavMesh Agent - slowing down?

Discussion in 'Editor & General Support' started by Vanz, Nov 13, 2022.

  1. Vanz

    Vanz

    Joined:
    Nov 19, 2013
    Posts:
    374
    I want an specter NPC to accelerate and move very quickly toward the player than stop about 1.5m away and then engage in melee combat (speed =10, acc = 10). The problem is the NPC gets too close or too far away from the player (I have auto braking enabled). I'm now playing with scripts where I drop the speed when the NPC is about 3m away and it works a bit better but when I move the NPC further back (it now has more speed) then it turns to crap again and gets too close again. I tried increasing the drag on the rigidbody of the NPC to see if this would help with slowing the NPC down (didn't see improvement).

    The real issue is that I don't understand or can find documentation on how the NPC slows down (NavMesh agent), per the documentation,

    https://docs.unity3d.com/Manual/class-NavMeshAgent.html

    "The agent will stop when this close to the goal location."

    what is "close"?? sometimes its right in the players face, sometimes its near my setting of 1.5m (which is a bit too far away, but if I lower this then it can be way to close if the NPC builds up speed).

    I'm doing this in VR, so when the NPC is too close it feels crappy....

    Any thoughts on how to handle this...

    Thanks,

    Vanz

    ps. I don't want to rewrite NavMesh, the pathfinding works well for me...
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,757
    You actually can disable the NavMeshAgent as it gets near a player, since obviously at that point they're right in striking range.

    At that point you take over the facing and chasing, which lets you maintain whatever precise distance you want.

    Then once the player gets more than X distance away, turn the agent back on again to chase the player down.

    I would suggest building in some hysteresis, such as disable the agent when distance is less than 3, but don't turn it back on until distance is greater than 4 (for example).
     
  3. Vanz

    Vanz

    Joined:
    Nov 19, 2013
    Posts:
    374
    thanks for the idea Kurt-Dekker
     
  4. Vanz

    Vanz

    Joined:
    Nov 19, 2013
    Posts:
    374
    is there a way to change the way Navmesh slows down (i.e. slow down faster or slower)? I would prefer to not turn NavMesh on/off, if I could tell NavMesh to slow down faster, that would be a cleaner solution.

    Thanks,

    Vanz
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,757
    You can set the speed of the agent.

    If you want to gradually change that speed rather than all-at-once, use this process:

    Smoothing movement between any two particular values:

    https://forum.unity.com/threads/beginner-need-help-with-smoothdamp.988959/#post-6430100

    You have currentQuantity and desiredQuantity.
    - only set desiredQuantity
    - the code always moves currentQuantity towards desiredQuantity
    - read currentQuantity for the smoothed value

    Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

    The code: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4