简体   繁体   中英

How to queue NavMeshAgents on entering a tile in Unity (based on path distance to tile)?

I'm making a game where multiple NavMeshAgents are trying to navigate to a specific ground tile. All agents should navigate to the edge of this tile, but only the first to reach the edge of the tile should enter it. The agent on the tile will stay there for a set time before leaving it, and will not return to the tile again afterward. Then, this cycle will repeat with the next closest agent entering the tile, and other eligible agents moving as close to the tile without going on top of it.

Note: The player controls obstacles in the game by moving walls, so a path's length can change at any moment if the obstacle layout is modified.

Possible ways that may work that I'm not sure how to implement:

  • Enabling and disabling invisible barriers
  • Calling CalculatePath() or SetDestination() repeatedly
  • Storing and using a List of the NavMeshAgents

If you want the other navmesh agents to stop, you can use the isStopped property.

From unity "isStopped" API - "If set to True, the NavMesh agent's movement will be stopped along its current path. If set to False after the NavMesh agent has stopped, it will resume moving along its current path."

You can find all navmesh agents you can use the Find function:

FindObjectsOfType(typeof(NavMeshAgent));
FindObjectsOfType<NavMeshAgent>();

You can also use a trigger barriers to trigger any action you want (just put a GameObject without mesh and set his collider to is trigger).

About sorting the agents, you can calculate the distance from the tile using the standard Distance function. You can create a dictionary or 2 parallel lists to sort the distances and in the same time keep the information about the agents (which agents has the smallest \ biggest distance).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM