I have to check for a free space in a virtual world of cubes (or mashes with a CubeCollider)
to initiate a new Object.
But what is the most elegant method to check for a free space?
Of course CheckCapsule or CheckSphere is exact what I’m looking for, only that they are not for Cubes.
Initiating the rigidbody with a collider and check a collision is not possible, because initiation is not a move, so there is no collision or trigger.
SweepTest is a nice function, but not for a new object.
So, is there something like CheckCube?
No built in function. Assuming you want to check some cubic space for an intersection, you can do it by ‘hand’. Generate a bounds for the space you want to check, then cycle through all the game object and check their colider.bounds against the bounds of the test area. A bit of untested code:
function IntersectBounds(bounds : Bounds, objects : GameObject[]) : boolean {
for (var go : GameObject in objects) {
if (bounds.Intersects(go.collider.bounds)) {
return true;
}
}
return false;
}
Note if you are going to be doing this a lot or have a large number of cubes, it might pay to build an array or generic List of colliders, and pass than in instead of an array of game objects. Note due to floating point inprecision, you might need to make the bounds test space just a smidgen smaller to avoid false positives.