- Home /
Random objects don't overlap
Hi guys! I wrote a script that create random planes at random positions colored randommly. But, some of these planes overlap each other and I don't want it. I would write a script that create random planes in random position but that don't overlap them each other. How can I do? Here's the script that I attached to my MainCamera.
#pragma strict
var s : GameObject;
var colors = [Color.red, Color.green, Color.blue];
function Start () {
StartCoroutine(creazione(1));
}
function Update () {
}
function creazione(tempo) {
while(true)
{
s = GameObject.CreatePrimitive(PrimitiveType.Plane);
s.renderer.material.color = colors[Random.Range(0, colors.Length)];
s.transform.position.y = 0;
s.transform.position.x = 0;
s.transform.position.z = Random.Range(0, 100);
yield WaitForSeconds(tempo);
}
}
You could spend a ton of time doing the maths to make the planes not overlap. Or you could give them colliders, check if they're collided when you spawn them, and then despawn them if they're collided.
I'd go with the second one. Just grab a cap on how many times the code tries to spawn stuff, so you don't get into infinite loop land.
Ok, I thought the same thing, but how can I write it in my javascript file that I wrote up in the question?
Overlap on the 'z', or overlap visually? Given that a Plane is 10x10, at most you can fit 11 planes using positions between 0 and 100 on the 'z'. Will your planes remain with localScale of (1,1,1)?
Overlap on Z axis and visually you can look only colored stripes of random planes that appear on the other planes that are created before. It happens because the planes doesn't stop creating, they are random, so they continue to appear in the scene with other random colors.
Answer by robertbu · Sep 27, 2014 at 12:10 AM
I hesitate to answer since I'm fuzzy on your criteria. But here is a script. It works by keeping a list of the Transforms of the objects created. When a new position is selected, it tests that position against the existing positions. Since a plane is 10 x 10 units, if a position is less than 10 units from an existing position, then there will be an overlap. It tries 50 times to find a position that fits. If it fails, it bails out of the coroutine.
#pragma strict
import System.Collections.Generic;
var list : List.<Transform> = new List.<Transform>();
var s : GameObject;
var colors = [Color.red, Color.green, Color.blue];
function Start () { StartCoroutine(creazione(1)); }
function creazione(tempo) {
while(true) {
var pos : Vector3;
for (var i = 0; i < 50; i++) {
pos = Vector3(0f,0f,Random.Range(0,100));
if (!CheckOverlap(pos)) break;
}
if (CheckOverlap(pos)) break;
s = GameObject.CreatePrimitive(PrimitiveType.Plane);
s.renderer.material.color = colors[Random.Range(0, colors.Length)];
s.transform.position = pos;
list.Add(s.transform);
yield WaitForSeconds(tempo);
}
}
function CheckOverlap(pos : Vector3) {
for (var t : Transform in list) {
if (Vector3.Distance(t.position, pos) < 10.0)
return true;
}
return false;
}
Well, I tried it this morning and it's perfect for my game! But now I have an other question: I have a ball that roll on these random colored planes, I also want make it colored randomly and I want to check when it rolls on the plane that has the same color. How can I do? Thanks a lot.
@GabboUnity - You can raycast down to access the plane, or given the code above, you can use the position of the ball and find the plane in the list. But this is really a new question. We like to keep each question focused on a single issue, so you should open a new question with a complete description of your problem.
Ok, in a little time I'll write this new question in another topic! Please follow my profile to see it and answer me. Thanks a lot.
Your answer
Follow this Question
Related Questions
randomly change between 6 colors C# 4 Answers
Switch between random colors 4 Answers
Random.Range(..) not working 1 Answer
How do you get different random numbers for each object array? 1 Answer