- Home /
Is there a physics.OverlapCube?
Hi all, first post on the forms here. I've been working with procedural mesh creation and texturing in order to create a Voxel based world in Unity, and I've been following the advice of this blog post. Anyway, the reason for my question is that I would like to be able to grab all of the meshes within a space, say a 5 X 5 X 5 cube, and store those values in an array in order to combine them into one mesh. OverlapSphere does a wonderful job of this, but uses a sphere which leaves out the meshes in the corners. If anyone knows of a build-in function I can use for this, or another method that would work, it would be much appreciated. Thanks in advance for the help!!!
Link for blog-post: http://www.sauropodstudio.com/how-to-make-a-voxel-base-game-in-unity/
This appeared in some version of Unity since you posted:
http://docs.unity3d.com/ScriptReference/Physics.OverlapBox.html
And its NonAlloc counterpart.
Answer by DaveA · Sep 12, 2012 at 10:19 PM
Probably a better way, but off the top: 1. Create a BoxCollider, set size position, check for collisions 2. Use an OverlapSphere, but set the radius to the diagonal length of the cube (eg 5 * 2^.3333) (cube-root of 2 times 5) to get a sphere's-worth of objects, then cull those by checking their bounds http://docs.unity3d.com/Documentation/ScriptReference/Renderer-bounds.html
Thanks Dave, I was leaning toward the BoxCollider myself, but was trying to figure out how to store all or the collisions in an array. I think I'm going to have each object that collides with the collider add itself to the array as opposed to having the collider add all objects that collide with it to the array. We'll see how it goes.
I have the same problem. What solution did you finally find?
Thank you
OverlapSphere does even check against the bounding box. Using Bounds.Intersects can be used to actually test AABB against AABB.
Yes finally I did that way. I used Sphere Overlap and then I used a bounds and I checked it with Bounds.Intersects.
It seems it works fine. Thanks
Answer by vanarebane · Apr 28, 2016 at 05:17 AM
There is now a OverLapBox. I googled and found your problem and then I tried to find it in API and found the OverlapBox.
Vector3 m_ExplosionBox = new Vector3(0.25f, 0.5f);
Collider[] colliders = Physics.OverlapBox (transform.position, m_ExplosionBox, transform.rotation, m_Mask);
More here: http://docs.unity3d.com/ScriptReference/Physics.OverlapBox.html
Your answer
Follow this Question
Related Questions
Determine whether or not model is "inside out" 2 Answers
Combining meshes (different materials) together for rotation/translation 1 Answer
Custom Mesh UV Problem 1 Answer
Mesh return vs out Mesh 2 Answers
Strange shading on procedural mesh 0 Answers