Hey,
I’m not quiet sure if understood your problem. As far as I got, was that you are spawning a number of entities, which spawn point is randomly calculated. So you are probably using something like Random.Range to set the position right?
And If I got it right you want to spawn the entites close to each other.
So what I would do is spawn the first entity randomly somewhere. Then I would run something like this to get the coordinates close to it.
var coordinates : Vector3; //or vector 2 whatever you are using for those
var spawnPoints : Array = new Array();
var spawnPointsInRange : Array = new Array();
var spawnPointsUnsorted : Array = new Array();
var firstSpawnPoint : int;
var entitesToSpawn : int;
var maxDistanceToFirstEnitity : int;
firstSpwanPoint = Random.Range(0,82);
For(var a : int = 0; a < 82; a++)
{
if(a != firstSpawnPoint) //Getting all the distances here
{
spawnPoints.Add(Vector3.Distance(coordinates[firstSpwanPoint], coordinates[a]));
spawnPointsUnsorted.Add(Vector3.Distance(coordinates[firstSpwanPoint], coordinates[a]));
}
}
spawnPoints.Sort(); //seeking closest point
for(var d : int =0 ; d < spawnPoints.length; d++)
{
if(spawnPoints[d] < maxDistanceToFirstEntity)
{
spawnPointsInRange.Add(spawnPoints[d]); //adding only the points which are close enough to first entity
}
}
for(var b : int = 0; b < spawnEntities + 1; b++) //seeking coordinate for each entity to spawn
{
var random : int = Random.Range(0, spawnPointsInRange.length); //will create a random number for the position inside the range
for(var c : int = 0; c < spawnPointsUnsorted.length; c++)
{
if(spawnPoints[random] == spawnPointsUnsorted
)
{
//Spawn your identity here at coordinats[c];
spawnPointsInRange.RemoveAt(random); // removes the value so that there is only on entity per coordinate
}
}
}
So something like this should work for that. I am not quiet sure if it is what you needed and it's not clean code, but it hopefully contains the basic thought on how to solve the problem.
Thanks for your answer. I'm trying to space the objects out as much as possible knowing that random spawning will not yield the ideal configuration for maximum space between the objects. I want to know the maximum space possible if the space was randomly used most ineffectively. Basically, I want to space as much as possible and still leave room for all the objects.
– CoalCzar