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

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?

What about creating an invisible object with a collider at the new location that matches the object you’re trying to instantiate? That invisible object could have a script attached that detects if it’s colliding with anything, and if so, prevents the other object from moving there.

I believe I have the answer for your 3-year-old question - Physics.BoxCast. Physics.BoxCast is a raycast in the shape of a box. Now I understand you don’t want to cast along a ray, you just want to check one position, but we can still use Physics.BoxCast for this. Now, a box cast, and any raycast, WILL NOT DETECT any collider it starts inside of. I thought at first that doing a box cast in your desired location with a travel distance of 0.0f would work, but it doesn’t detect anything it starts out colliding with, only things it moves into. So what you have to do is start your box cast right beside/above/adjacent to wherever you want it to detect collisions in, and then have the direction + distance set so that it will move into your desired position and then stop. This should work to detect anything in that space. For example, the code below detects if there is anything in a 1x1x1 cube centered around (0, 5, 0). It starts out in (0, 5, -1) and then moves 1.0f units in the forward (z) direction, to stop at (0, 5, 0).

RaycastHit hitInfo;
if (Physics.BoxCast(new Vector3(0, 5, -1), new Vector3(0.5f, 0.5f, 0.5f), Vector3.forward, out hitInfo, Quaternion.identity, 1.0f
{
      print(hitInfo.collider.name);
}
else
{
      print("Nothing hit");
}

Now this still won’t detect something if that thing was so big that the boxcast, even moved to the side, was STILL inside the object. So you’ll have to tweak this method to work with your specific situation.

As for options to detect collisions when the ray does start inside the object, it seems to get more complicated, but here is a page where someone discusses possible ways to do this with a raycast, if you want to look into it yourself.
https://forum.unity.com/threads/raycast-not-finding-objects-collider.323109/

Hope that helps the people that see this!