0
\$\begingroup\$

In the screenshot group of 4 agents (in red) are staying on the waypoint(blue) and they just rotating on the waypoint. The other agents keep moving the waypoints.

Agents

I'm creating waypoints and agents (NavMeshAgent):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InstantiateObjects : MonoBehaviour
{
    public GameObject prefab;
    public Terrain terrain;
    public float yOffset = 0.5f;
    public int objectsAmount;
    public bool parent = true;
    public bool randomScale = false;
    public string tag;
    public string name;

    public Vector3 RandScaleMin;
    public Vector3 RandScaleMax;

    private float terrainWidth;
    private float terrainLength;
    private float xTerrainPos;
    private float zTerrainPos;
    private GameObject clonedObject;
    private ObjectPool objectPool;

    public void Start()
    {
        //Get terrain size
        terrainWidth = terrain.terrainData.size.x;
        terrainLength = terrain.terrainData.size.z;

        //Get terrain position
        xTerrainPos = terrain.transform.position.x;
        zTerrainPos = terrain.transform.position.z;

        generateObjectOnTerrain();
    }

    public void Update()
    {

    }

    public void ReleaseObjects()
    {
        GameObject[] allobj = GameObject.FindGameObjectsWithTag(tag);
        for (var i = 0; i < allobj.Length; i++)
        {
            objectPool.ReturnInstance(allobj[i]);
            allobj[i].hideFlags = HideFlags.HideInHierarchy;
        }
        generateObjectOnTerrain();
    }

    public void generateObjectOnTerrain()
    {
        objectPool = new ObjectPool(prefab, objectsAmount);

        for (int i = 0; i < objectsAmount; i++)
        {
            //Generate random x,z,y position on the terrain
            float randX = UnityEngine.Random.Range(xTerrainPos, xTerrainPos + terrainWidth);
            float randZ = UnityEngine.Random.Range(zTerrainPos, zTerrainPos + terrainLength);

            float yVal = Terrain.activeTerrain.SampleHeight(new Vector3(randX, 0, randZ));

            var randScaleX = Random.Range(RandScaleMin.x, RandScaleMax.x);
            var randScaleY = Random.Range(RandScaleMin.y, RandScaleMax.y);
            var randScaleZ = Random.Range(RandScaleMin.z, RandScaleMax.z);
            var randVector3 = new Vector3(randScaleX, randScaleY, randScaleZ);

            //Apply Offset if needed
            yVal = yVal + yOffset;

            clonedObject = objectPool.GetInstance();

            if (randomScale == true)
                clonedObject.transform.localScale = randVector3;//new Vector3(randScaleX, randScaleY, randScaleZ);

            if (parent)
            {
                GameObject parent = GameObject.Find(name);
                clonedObject.transform.parent = parent.transform;
            }

            clonedObject.tag = tag;
            clonedObject.transform.position = new Vector3(randX, yVal, randZ);
        }

        if (prefab.name == "AgentPrefab")
            AgentsComponents.StartInit();
    }
}

Then adding two components to each agent:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class AgentsComponents
{
    private static GameObject[] objectsfound;

    public static void StartInit()
    {
        objectsfound = GameObject.FindGameObjectsWithTag("Agent");

        for (int i = 0; i < objectsfound.Length; i++)
        {
            objectsfound[i].AddComponent<NavMeshAgent>();
            objectsfound[i].AddComponent<AgentControl>();
        }
    }
}

And the AgentControl script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class AgentControl : MonoBehaviour
{
    public List<Transform> points = new List<Transform>();
    private int destPoint = 0;
    private NavMeshAgent agent;

    void Start()
    {
        agent = GetComponent<NavMeshAgent>();

        var agentsDestionations = GameObject.FindGameObjectsWithTag("Waypoint");

        for (int i = 0; i < agentsDestionations.Length; i++)
        {
            points.Add(agentsDestionations[i].transform);
        }

        // Disabling auto-braking allows for continuous movement
        // between points (ie, the agent doesn't slow down as it
        // approaches a destination point).
        agent.autoBraking = true;

        agent.speed = Random.Range(10, 50);

        GotoNextPoint();
    }

    void GotoNextPoint()
    {
        // Returns if no points have been set up
        if (points.Count == 0)
            return;

        // Set the agent to go to the currently selected destination.
        agent.destination = points[destPoint].position;

        // Choose the next point in the array as the destination,
        // cycling to the start if necessary.
        destPoint = (destPoint + 1) % points.Count;
    }


    void Update()
    {
        // Choose the next destination point when the agent gets
        // close to the current one.
        if (!agent.pathPending && agent.remainingDistance < 0.5f)
            GotoNextPoint();
    }
}

I set autoBraking to true since I want the agents to slow down when getting close to each waypoint.

What I want to do is to make all the agents to move between all the waypoints. Either is there are 10 agents or 500 some of them stay at some waypoint and not continue to the next one. I waited like 2-3 minutes and they didn't continue.

I didn't change anything in the NavMeshAgent properties except the autoBraking set it to true.

\$\endgroup\$
0
\$\begingroup\$

You want to increase the Stopping Distance so the agent will complete the navigation.

The crowded agents all fighting and auto braking to get .5f away from the destination so they will just spin around.

Increasing the stopping distance will allow a bit of leeway for when a destination get's crowded and it should move to the next point just as expected.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.