Home > Software engineering >  How Do I Generate points in a circle based on the size of objects colliders and the number of object
How Do I Generate points in a circle based on the size of objects colliders and the number of object

Time:12-14

I am making an RTS game were im trying to handle unit formation.

I've already tried finding the center of all units and adding that to the position were you click and it works but they remain spread out. So if you could help me out id appreciate it.

CodePudding user response:

For a basic solution, you could use Unity's Random.insideUnitCircle method, which returns a random point inside or on a circle with a radius of 1. That will give you a random point you can generate per unit within your circle.

If you're looking for functionality to make the points equidistant from one another, then there are many solutions out there, but something simple would be:

private List<Vector3> _points = new();

for (var i = 0; i < numberOfPoints; i  )
{
    var angle = i * Mathf.PI * 2 / numberOfPoints;
    var point = new Vector3(Mathf.Cos(angle), Mathf.Sin(angle), 0) * radius;
    _points.Add(point);
}

Where numberOfPoints is the number of units you have, or unit Colliders, and radius the size of your selection circle.

  • Related