Quantcast
Channel: Questions in topic: "random spawn"
Viewing all articles
Browse latest Browse all 125

Detect if an object will collide if it moves to a position

$
0
0
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?

Viewing all articles
Browse latest Browse all 125

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>