simplest way to add a minimum distance or gap between instantiated objects
Hello, I am currently instantiating a prefab with a random amount and random locations, but as you would expect they sometimes instantiate too close to each other so, which would be the most simplest method to get them to have a minimum space in between them so far i have this:
void Start () { int starNum = Random.Range(1, 5);
for (int i=0; i < starNum; i++){
float Randomvalue = Random.Range(-1.0f, 2.0f);
starlocation = new Vector3(Randomvalue*gap, 0.5f, -2f);
GameObject starInstance = (GameObject)Instantiate(star, starlocation, Quaternion.identity);
GameObject[] stararray = GameObject.FindGameObjectsWithTag ("star");
starsLeft = stararray.Length;
print(starsLeft);
}
}
Answer by PizzaPie · Nov 17, 2018 at 01:22 PM
Vector3 starLocation;
GameObject[] starArray;
float minStarDistanceX;
GameObject star;
void Start()
{
int starNum = Random.Range(1, 5);
starArray = new GameObject[starNum];
float starExtentX=0f; //or renderer bounds but will require to apply changes based on scale, or you can assign the value manually
List<float> starXPositions = new List<float>();
for(int i = 0; i < starNum; i++)
{
bool isAccepted = true;
float randomXPos = 0;
int safeCount = 0;
do
{
randomXPos = Random.Range(-1f, 2f); //min and max X positions
foreach (float f in starXPositions)
{
if (Mathf.Abs(f - randomXPos) < starExtentX + minStarDistanceX) //check distance from existing ones
{
isAccepted = false;
break;
}
}
safeCount++;
if (safeCount > 1000) //in case it can not find any accepted positions
{
Debug.LogError("Unable to find proper X position"); //or throw an exception
break;
}
} while (!isAccepted);
starArray[i] = Instantiate(star, new Vector3(randomXPos, 0.5f, -2), Quaternion.identity);
starXPositions.Add(randomXPos);
if(i==0)
starExtentX = starArray[0].GetComponent<Collider>().bounds.extents.x;
float starsLeft = starNum - i + 1; //probably you meant this value
print(starsLeft);
}
}
Generates positions till it gets one that is not too close to other stars.
Optimal conditions to function properly are: big space (min and max X positions should be quite far) small ammount of objects to be created in given space.
hi, I've tried your solution it looks great just can't understand why it's not working the colliders for each object are overlapping? any ideas?
Tested it and... well as expected fails most times to generate good values, also it seems collider.bounds.extents.x on a prefab is 0 so you have to pick the value once the first object is created. Edited to fix the last one.
what can be done to improve the values this solution seems most appropriate for what I'm trying to achieve, also with the edited version of code the colliders are still overlapping?
Your answer
Follow this Question
Related Questions
Using foreach to remove and delete bullet in List - C# 3 Answers
How can I check the color of GameObjects in an Array and print out their color? 0 Answers
Retrieving values from list of Scriptable Objects 1 Answer
InvalidOperationException: Collection was modified; enumeration operation may not execute 1 Answer