Avoid Other units

This may be a simple question, but I haven’t found anything to solve this:

How can I achieve the avoidance of units based on their colliders? Currently, they’re all moving to the same spot.

Example: I have 3 Units selected ans sent them tp one point: they move all to this point and are stuck within each other


This is my movement script for (multiple) units so far:

 private void MakeMove(Vector3 dir)
    {
        inCover = false; 
        transform.LookAt(dir);                                                       
        float scaledSpeed =  1 / moveSpeed;
        transform.position = Vector3.Lerp(transform.position, dir, scaledSpeed);
        if (transform.position == destination) moving = false;
        CalculateBounds();
    }

This is used for walking on the given path to the next checkpoint.
The code below is used to send the selected units to the desired location:

   foreach (WorldObject worldObject in player.SelectedObjects)
                    {
                        if (worldObject.GetComponent<Unit>() != null)
                        {
                            worldObject.GetComponent<Unit>().MouseClick(hitObject, hitPoint, player);
                        }
                        else
                        {
                            worldObject.MouseClick(hitObject, hitPoint, player);
                        }
                    }

MouseClick returns the destination based on the hit.point coordinates and starts the path:

    seeker.StartPath(transform.position, _destination, OnPathComplete);

My attempt was to hard-code the distances, so e.g send one unit to the clickpoint and all other units get a random point close to this, but this doesnt work correctly and also has problems, if there are already obstacles at/near the given position

Hi

Different games handle these scenarios in very different ways depending on the type of game.
One could use local avoidance (available in the pro version, see http://arongranberg.com/astar/docs/local-avoidance.php) however usually one would still want to order them to move to different positions.
There is an API for this. See the GetPointsAroundPoint method here http://arongranberg.com/astar/docs_3.8_new/pathfinding/pathutilities.html#GetPointsAroundPoint. This is exactly the code that is used in the game Folk Tale to select the points that units should move to.

Thank you for your reply. I already checked out the docs before writing this but I have sadly no clue how to implement this.

Here is an example of how it is implemented in FolkTale:

public static List<Vector3> UnitRegroupingAtPoint (List<Character> units, Vector3 destination) {
	GraphNode node = AstarPath.active.GetNearest(destination).node;

	if (node == null) return null;

	List<Vector3> pts = new List<Vector3>();

	for (int i = 0; i < units.Count; i++)
		pts.Add(units[i].transform.position);

	Pathfinding.PathUtilities.GetPointsAroundPointWorld(destination, AstarPath.active.astarData.recastGraph, pts, 0, 6);

	return pts;
}

That methods takes in a list of units and returns the points that the units should move to (first unit moves to the first point in the list etc.).

Thanks, thats very similar to my approach, I just made my own, not very fancy method for the GetPointsAroundPointWorld part.

Thank you :slight_smile:

Yes there is one in the RVO example scene as well as in the RVO Lightweight example scene.