Multiple units going at the same position

Hello! I’m currently working on a RTS game and I’m facing some issue having multiple units going to the same position.

Basically, I have a grid graph which is on top of my terrain (which is a simple plane in 3D). I do a Raycast on my terrain and I set the destination of my unit to go to that exact position (node).

The issue is that if I have multiple units having the same destination, some strange behavior happened (which is totally normal because they will always fight to reach the destination). I would like my units to find the closest available node next to the destination position node. I took a look at the documentation and some threads on the forum and I found some people having the same issue as mine. I found this method PathUtilities.GetPointsOnNodes which I think is the solution. However, this method must have a list of Node as the first parameter while I only have the current destination node. Is there a way to achieve this,

Here’s my code :

/// <summary>
/// Give order to the units 
/// </summary>
private void GiveOrders()
{
   RaycastHit hit;

   if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100))
   {
       for (var i = 0; i < SelectedUnits.Count; i++)
       {
          [...] 
          SetUnitMoveOrder(SelectedUnits[i], hit);         
       }
   }
}
/// <summary>
/// Make the units move to a specific point
/// </summary>
/// <param name="unit"></param>
/// <param name="hit"></param>
private void SetUnitMoveOrder(Unit unit, RaycastHit hit)
{
    var nodePosition = AstarPath.active.GetNearest(hit.point).position;
    var node = AstarPath.active.GetNearest(hit.point).node;
  
    unit.AIPath.destination = nodePosition;
    unit.UnitStateMachine.ChangeState(unit.UnitWalkState);      
}

I simply put the SetUnitMoveOrder in the the example and remove all other types of order such as cosntruct building, attack, etc. to ease the explanation and focus on this specific issue.

Hi

GetPointsOnNodes will generate a bunch of random points on an existing set of nodes. It’s not quite doing what you want.

Instead, maybe PathUtilities - A* Pathfinding Project or PathUtilities - A* Pathfinding Project is what you want. This is used in the RTS example scene (beta only) to avoid all agents getting the same destination.