I’m working on some procedural generation and I want to check if there is a collision at a particular point, if a it finds one, I want to skip my logic that instantiates that game object. I assume the best way to do this is to create a box, check the colliders it hits, and then evaluate if colliders were hit.
Depends. What is it you are trying to do? You seem to be spawning some objects and want to only spawn them when they dont overlap with any previously spawned objects? Are we just talking about procedural object placement here, or some procedural generation?
If we are talking about procedural object placement, then you may wanna look into algorithms like Poisson Disc Sampling, which allows you to densely pack pseudo randomly placed objects with known diameters.
If we are talking about procedural generation of some sort, then you need to provide more information for what exactly it is you are doing. So far i only know that you are placing objects and want to know if it collides at some point.
For the statement “I want to check if there is a collision at a particular point” the proper solutions seems to just use Raycasts. Or more precisely, Physics.RaycastAll, since it returns all hits for a given raycast, not just the first. If it’s one, then you have one object at that position, if it’s two then there are already two objects overlapping at that position.
I’m building a procedural map generator based on existing room prefabs. This particular exercise, I’m doing some map cleanup after spawning rooms and corridors and checking hallways that don’t have a closed end. In the event I find a hallway without a closed end, I go through an evaluation of :
Can I spawn an 8x8 endcap ( single entry room)?
Can I spawn a 4x4 endcap?
Can I spawn a 2x2 endcap?
Don’t spawn if any of the above conditions are not met.
I need to check the size of the endcap to the spawnpoint and evaluate if there is a collision, if there isn’t a collision we’ll go ahead and spawn it. Otherwise continue down the list trying different sizes.
Okay, now i have a clearer understanding of what you are trying to achieve. If my understanding of your problem is correct, then this problem should only occur because the “open ended corridor” may point towards some previously placed room, and you need to know if there is enough space left to place a new room to close it off, right?
If so, then you should be able to use raycasts as follows to get the distance between the corridor and the next room in the direction of its opening. Cast a ray, starting at the position of the open end of your corridor, towards the direction of the opening. This does require you to know in which direction the corridor is opened, but from what you wrote i assume you do. It also requires you to know at which point precisely the opening (or end of the corridor) is, which i assume you can calculate in your situation.
Now calculate the distance between the hit.point and the starting point. If no hit was found, distance can be assumed to be infinite for this scenario. If distance is >= 8 you can spawn your 8x8 room, else if it’s >= 4 you can spawn your 4x4 room, and so on.
Would something like this work for your situation?
Yeah I think it would work. Would it be better to use extent properties and check the size of the prefab I’m spawning? lets say I’m spawning an 8x8 and draw a raycast from top left corner to bottom right corner, if nothing hits, then in theory it should be ok right?
This might illustrate it better, the red X locations are where an 8X8 wouldn’t fit. However a 4x4 might fit a couple of those locations.
The colliders also needed to be adjusted on each object, leaving them at full values didn’t work for me, so I had to bring the collider values in some.
Example:
On a 1x1 square 2D sprite, I would use the box collider size values of .9 x, .9y.
Glad you solved your problem. The image would have been useful before
The approach i listed above would have worked as well, but you’d need to check for distance not just forwards, but also right and left, which is something i overlooked in my approach. Anyways, as long as it works thats great!