- Home /
Instantiate a 2D world question
Hello! I am trying to instantiate a bunch of prefabs on the X-axis and have done so without much trouble using a simple forloop code.
However my current problem is that the objects that I am instantiating at random varies in size. So sometimes what happens since the whole spawning of the objects is at random. Is that they spawn inside eachother. This makes it extremely difficult at times to actually play the game.
I am wondering if there is a better way of instantiating objects in a line where they wont be instantiated inside eachother.
The code I'm currently using is quite simple being:
for (int x = 0; x < worldSize; x++)
{
Vector3 pos = new Vector3(Random.Range(400, 800) * x , Random.Range(-300, -450), 0);
Instantiate(WorldObjects[Random.Range(0, WorldObjects.Length -1)], pos, Quaternion.identity,
this.transform);
}
I've been fiddling around with the vector3 position but it doesnt quite solve my problem since it's quite random.
Thanks for your help! :)
Answer by toddisarockstar · Sep 06, 2018 at 12:22 AM
if you want items touching in a line the math is simple.the distance between two objects should be half of the the first item's width plus half the touching items width.
here is an example:
void Start () {
float x = 0;
float randomsize = 0;
int i = 10;while(i>0){i--;
x=x+randomsize*.5f;//adding half of the previos item's size;
randomsize = Random.Range(2f,6f);
x=x+randomsize*.5f;//now adding half of the current item's size;
GameObject g = GameObject.CreatePrimitive(PrimitiveType.Cube);
g.transform.renderer.material.color = new Color(Random.Range(0f,1f),Random.Range(0f,1f),Random.Range(0f,1f),1);
g.transform.localScale = new Vector3(randomsize,randomsize,randomsize);
g.transform.position = new Vector3(x,0,0);
}
}
Answer by tormentoarmagedoom · Sep 05, 2018 at 06:43 PM
Good day.
The solution can be very simple.
You only need to check the distance between the position (pos) and all instantiated objects. You only need to have an array or list with all the instantiated objects (or you add them to a list when instantiate them, or you make them have the same tag)
Imagine all instantiated objects have the tag "Obj"
Then only need to check the distance betwee the position you are going to use and all the objects taged as "Obj". If some of the distances is lower than a value (for example 100), recalculate the position you want. You will need to create a method for cheking this.
for (int x = 0; x < worldSize; x++)
{
Vector3 pos = new Vector3(Random.Range(400, 800) * x , Random.Range(-300, -450), 0);
while (ObjectTooClose(pos) == true)
{
pos = new Vector3(Random.Range(400, 800) * x , Random.Range(-300, -450), 0);
}
Instantiate(WorldObjects[Random.Range(0, WorldObjects.Length -1)], pos, Quaternion.identity,
this.transform);
}
[...]
public bool ObjectTooClose(Vector3 PositionToCheck)
{
foreach (GameObject OneObject in GameObject.FindObjectsWithTag("Obj")
{
if (Vector3.Distance (OneObject.transform.position, PositionToCheck) <100)
{
return true;
}
}
return false;
}
Sooooo, it checks for all objects distance to the position you want to use. If find some object too close, it returns true, so the random position is calculated again. But if does not find any object too close, it can continue and instantiate a new object.
This method have a risk, if you spawn too much objects, maybe can not find any position away from other objects, and the while loop will be infinite...
But this is the idea, (Maybe the code have some error, so check it, understand what is doing and solve!)
Bye!
Thank you! That's a very detailed and specific explanation and I think I understand what you're getting at :) I'll give it a shot and see if it can help me!
Thanks again for the quick reply ^^
Your answer
![](https://koobas.hobune.stream/wayback/20220612174121im_/https://answers.unity.com/themes/thub/images/avi.jpg)