Search Unity

SetDestination() working only one agent at a time?

Discussion in 'Scripting' started by AntitheticalElysium, Apr 26, 2022.

  1. AntitheticalElysium

    AntitheticalElysium

    Joined:
    Jan 11, 2022
    Posts:
    2
    Hello!

    After some testing, I've noticed that my NavMesh agents set a direction one at a time. This isn't very obvious with only a few but once 50 or more agents are active, it becomes painfully obvious as they all wait for their turn to start moving.

    Is there a way around this? Perhaps an alternative to the SetDestination method?

    Thank you!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    Most likely just a bug in your code. Want to share it, and a video?
     
  3. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    should share the code, and also picture of what your navmesh looks like could help as well
     
    PraetorBlue likes this.
  4. AntitheticalElysium

    AntitheticalElysium

    Joined:
    Jan 11, 2022
    Posts:
    2
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class NavMesh : MonoBehaviour
    7. {
    8.     private NavMeshAgent agent;
    9.     private Animator animator;
    10.  
    11.     [SerializeField] private Transform movePos;
    12.  
    13.     public GameObject[] waypoints;
    14.     public int waypointInd;
    15.     public int randomStop;
    16.  
    17.     void Start()
    18.     {
    19.         agent = GetComponent<NavMeshAgent>();
    20.         agent.updatePosition = true;
    21.  
    22.         waypoints = GameObject.FindGameObjectsWithTag("Waypoint");
    23.         waypointInd = Random.Range(0, waypoints.Length);
    24.         randomStop = Random.Range(0, 3000);
    25.  
    26.         animator = GetComponent<Animator>();
    27.     }
    28.  
    29.     void Update()
    30.     {
    31.         animator.SetFloat("Speed", agent.velocity.magnitude);
    32.      
    33.         if (randomStop > 2998)
    34.             Pause();
    35.         Patrol();
    36.         randomStop = Random.Range(0, 3000);
    37.     }
    38.  
    39.     void Patrol()
    40.     {
    41.         if (Vector3.Distance(this.transform.position, waypoints[waypointInd].transform.position) >= 10)
    42.             agent.SetDestination(waypoints[waypointInd].transform.position);
    43.         else
    44.             waypointInd = Random.Range(0, waypoints.Length);
    45.     }
    46.  
    47.     void Pause()
    48.     {
    49.         agent.isStopped = true;
    50.         StartCoroutine(Coroutine());
    51.         agent.isStopped = false;
    52.         agent.SetDestination(waypoints[waypointInd].transform.position);
    53.  
    54.     }
    55.  
    56.     IEnumerator Coroutine()
    57.     {
    58.         yield return new WaitForSeconds(Random.Range(2, 4));
    59.     }
    60. }
    Here's the code. The bottom two functions both serve to stop the agent at random intervals for a random number of seconds. The rest is what makes the agents go from point A to point B. I'd appreciate any help and thanks for the replies!

    I'll see what I can do for a video.
     
  5. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    well your Coroutine is having absolutely zero effect, and you are setting destination 1 to 2 times every frame so think you really need to work out what you are trying to do and take a new approach
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    This in itself will trigger the agents to stop and stagger around because you keep bothering them and they can never get to work solving the path.

    If you fix your code above and it is still not doing what you want, there are controls you can twiddle to give more processing power to the navigation engine, but I will guess offhand that Unity's default settings are probably fine for 99% of reasonable uses.

    https://docs.unity3d.com/ScriptReference/AI.NavMesh.html
     
  7. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    more or less you will want to be setting a destination once, let them get close to it, you can do that by checking the agents remainingDistance, then once they reach the destination you can give them a new thing to do.

    also about the Coroutine, coroutines do not block, you essentially asked it to spin up a coroutine that waits 2 to 4 seconds then ends. but the logic after the start coroutine line still executes and does not wait since its not part of the coroutine
     
    Kurt-Dekker likes this.