Hi,
I am trying to create a randomly generated level where i am stuck on stopping objects from overlapping. I am trying to detect if an object is in the space that the new object is being positioned. I have had the idea to raycast the area with two rays, one to check the width and one to check if the previous ray has not started inside an object.
float X = (int)Random.Range (0, 30);
float Y = (int)Random.Range(-10f, 8f) + 0.5f;
float Size = (int)Random.Range(1, 20);
Ray ray = new Ray(new Vector3(X - Size/2 - 0.005f, Y, 0), Vector3.right);
Ray rayFront = new Ray (new Vector3 (X - Size / 2, Y, -10), Vector3.forward);
while (Physics.Raycast(ray, Size) || Physics.Raycast(rayFront, 15))
{
X = (int)Random.Range (0, 30);
Y = (int)Random.Range(-10f, 8f) + 0.5f;
Size = (int)Random.Range(1, 20);
ray = new Ray(new Vector3(X - Size/2 - 0.005f, Y, 0), Vector3.right);
rayFront = new Ray (new Vector3 (X - Size / 2, Y, -10), Vector3.forward);
}
Platform.transform.position = new Vector3(X, Y, 0);
Platform.transform.localScale = new Vector3(Size, 1, 1);
but objects still seem to overlap. Does anyone know how objects could be detected in the same frame as the Instantiation of the new object?
↧