- Home /
avoid overlap problem when randomly instantiating
Hi , I wrote down a script to randomly put some Gems(Cubes) to my stage , I want to have 20 gems scattered , but sometimes they overlap . what's the best way to solve it . ( I dont want to destroy Overlapping Gems).
Here's my code :
public class RandomPosition : MonoBehaviour {
float rangeY = 3 ;
float rangeX = 4 ;
float y = 0.4f ;
int count = 20 ;
public GameObject gem ;
void Start () {
Vector3[] arr = new Vector3[count]; // random Position vector //
for (int i=0 ; i < count ;i++)
{
arr[i] = new Vector3 (Random.Range (-rangeX,rangeX),y,Random.Range (-rangeY,rangeY)); // random Position vector //
Instantiate (gem,arr[i],transform.rotation); // Creates random gems on Stage //
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Why do objects keep spawning on top of one another? 0 Answers
Instantiate Random 3D Objects/platforms that dont overlap 1 Answer
Drop item from airplane 1 Answer
Instantiate Prefab at random 1 Answer