Hey, so the randomizing part is easy but I couldn’t get the objects (with Colliders) not touch each other while spawning at a new location (imagine background mountains that shuffle and relocate in an endless runner)
I tried to use Bounds.Intersects but failed… please take a look:
Bounds spawnBounds = this.GetComponent<Renderer> ().bounds;
for(int i=0; i<backgrounds.Length; i++) {
Transform aBackground = backgrounds *;*
-
Transform pBackground;*
-
if (i == 0) {*
-
pBackground = aBackground;*
-
} else {*
-
pBackground = backgrounds [i - 1];*
-
}*
-
if (!aBackground.gameObject.GetComponent <Renderer> ().bounds.Intersects (pBackground.gameObject.GetComponent <Renderer> ().bounds)) {*
-
aBackground.position = new Vector3 (Random.Range (spawnBounds.min.x, spawnBounds.max.x), aBackground.position.y, aBackground.position.z);*
-
print ("not intersecting");*
} else {
-
aBackground.position = new Vector3 (pBackground.gameObject.GetComponent <Renderer>().bounds.max.x+1.0f, aBackground.position.y, aBackground.position.z);*
-
}*
[85698-screen-shot-2017-01-10-at-14536-pm.png|85698]*
the green bounding box belongs to the ‘spawnBounds’ object which the mountain boxes are its children…
Thanks!
*