Search Unity

Question Laggy & Weird Agent Movement

Discussion in 'Navigation' started by OzgurGurbuz, Nov 18, 2022.

  1. OzgurGurbuz

    OzgurGurbuz

    Joined:
    Jun 18, 2019
    Posts:
    38
    Hello dear friends, I hope you are all fine. :)
    We're currently experiencing laggy Nav Mesh Agent movement and we've no idea what is the reason behind it.
    Our project has a large terrain which has many trees and buildings on it (around 500 for now). We needed our objects' Navmesh to be Carved in order to achieve a nice auto path finding because we've seen Navmesh Obstacle by itself is glitchy and our units got stuck at trees' or buildings' corners time to time. Before carving the environment units are moving in an acceptable phase on the terrain but after the carving weird behaviours occur such as movement lag as shown in the video. We've tried to tweak the Navigation Bake settings, and it seems Voxel Size or Min Region Area isn't changing anything.

    Our knowledge on AI is highly lacking therefore we got stuck. Anyone out there willing to help these poor souls?


    Here's the problem:
    (At the end of the video you'll see one of the moving unit's N.M Obstacle as disabled, that's because the unit was still moving. It won't be active until the unit gets to stop.)




    Current settings:

    1- Usage of N.M Agent and N.M Obstacle together:
    We only carve ally units when their path is completed or no longer exists. We've implemented it from this video:



    2- Navigation
    Navigation.jpg

    3- Nav Mesh Agent
    Nav Mesh Agent.jpg

    4- Nav Mesh Obstacle
    (Move Threshold and Time To Stationary have Zero effects on the lag.)

    Nav Mesh Obstacle.jpg
     
  2. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300
    Every time when you carve the navmesh it resets the current path of all agents and the re-path takes a little time. That is why you see a lag.
    Having obstacles on agents isn't going to work if you plan to have big battles anyway as carving happens permanently and your agents will get stuck as their path is reset again and again...
     
    OzgurGurbuz likes this.
  3. OzgurGurbuz

    OzgurGurbuz

    Joined:
    Jun 18, 2019
    Posts:
    38
    Waow! I didn't know it was re-pathing all the agents almost every frame. Thanks for the information.
    Though, disabling carve and having only Obstacles causing units to push each other while battling. Then they're getting stuck at objects because they do not know how to find another pathways even though the Auto Repath option is enabled.

    We also tried to implement NaviPro asset (deprecated recently) but it turned out to be a bad idea because this time agents were passing each other when they aim one another rather than stopping at a certain distance. We're still doing our experiments but no good solution yet to be found. :confused:
     
  4. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300
    Pushing can be dealt with using priorities correctly as well as a higher resolution navmesh (smaller voxel size). I never had a problem that agents pushing another one out of the navmesh tbh.
     
    OzgurGurbuz likes this.
  5. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    Here is a video from GDC which have alot of information about "How" they approach the AI Pathing:


    Its pretty advanced and im wondering how the performance is on a large amount of AI units
     
    OzgurGurbuz likes this.
  6. OzgurGurbuz

    OzgurGurbuz

    Joined:
    Jun 18, 2019
    Posts:
    38
    I found a way to prevent SetDestination lag by using NavMesh.CalculatePath.
    It is quite hard for agents to find a proper way in a crowd in one frame.
    So I first check if any path has been found:

    Code (csharp):
    1. agent.SetDestination(transform.position); //Don't forget to initiate the first movement.
    2. NavMeshPath path = new NavMeshPath();
    3. if(NavMesh.CalculatePath(transform.position, targetPos, NavMesh.AllAreas, path))
    4. {
    5.      agent.SetPath(path);
    6. }
    If not I check it again after one frame later:

    Code (csharp):
    1. else
    2. {
    3.      StartCoroutine(Coroutine());
    4.      IEnumerator Coroutine()
    5.      {
    6.           yield return null;
    7.           if(path.status == NavMeshPathStatus.PathComplete)
    8.           {
    9.                 agent.SetPath(path);
    10.           }
    11.      }
    12. }
    If the path isn't completed than you can open another else field and make your decisions inside of that.
    This works like a charm for me. I wrote this myself so it could be weird, but it works.
    I hope someone can benefit from it.​
     
    Floresssss likes this.