- Home /
Test if BoxCollider is within another BoxCollider?
Hello Unity Community,
Besides writing my own collision detection or using the OnTriggerEnter/Stay/Exit or OnCollisionEnter/Stay/Exit is there another method to determine if a BoxCollider has a part of another BoxCollider inside it touching it?
-Ethan
Answer by ATMEthan · May 29, 2013 at 09:12 PM
And the answer is yes! You can use the BoxCollider.bounds.intersect method.
something like this:
if(topHeaderBoxCollider.bounds.Intersects(currentHeader.boxCollider.bounds))
I didn't check if there any other methods. I just know that once I wrote this question I remember about the bounds variable in the BoxCollider and thought that should have something and it did.
The downside of this solution is that I believe it only works properly if both BoxColliders are axis-aligned. I still can't find a good general solution to check if two BoxColliders (or other type of colliders) intersect without needing to attach non-kinematic rigid bodies.
To solve the problem when Bounds are not mutually axis aligned, you could do something like this: Define 2 bounds each in the local space of 2 objects/transforms. They are each axis aligned in their local space, but not mutually axis aligned. For each corner point in bound1 (8 box points), transform the point, transform1. localToWorld.$$anonymous$$ultiplyPoint(), then transform2.worldToLocal.$$anonymous$$ultiplyPoint(). Test if the transformed point is within bound2 - bound2.Contains(xformedPoint). You'll also need to do the same with each point of bounds2, xform to the space of bound1, test if within the bound. If any point is within a bound, then break, no need to test the rest of the points.
Answer by andreim44 · Mar 05, 2016 at 04:04 PM
I'm using the OnTriggerStay() method with the bounds.Contain() method to check this:
void OnTriggerStay (Collider other)
{
if (other.bounds.Contains(myCollider.bounds.min)
&& other.bounds.Contains(myCollider.bounds.max))
{
// Inside the box collider
}
}
Worked for me. Thanks man!!! I never knew there were functions for checking this.
Answer by UnPluks · Mar 16, 2016 at 05:43 PM
Hi! I know this topic is very old, but I would like to know what you guys think about this You can try to write your own function, using the vertices of you cube
void CheckBoxIntersection(GameObject go){
//Get the mesh you want to check
Mesh mesh = go.GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
int i = 0;
while (i < vertices.Length) {
//Raycast from the middle to each vertex
Ray ray = new Ray(go.transform.position, transform.TransformPoint(vertices[i])
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
Debug.Log("Intersecting " + hit.collider.name);
}
}
Or if you want to go more precise, you could raycast three times per vertex, racasting from each vertex to the adjacent ones, or something like that, I haven't actually try this, I am just trying to brainstorm with you guys. Any thoughts about this would be nice
Answer by highboy_coupe · Apr 04, 2017 at 05:02 AM
In addition to the comment above by @UnkPluks, a slight amendment would be:
for (int i = 0; i < vertices.Length; i++)
{
//Raycast from the middle to each vertex
Ray ray = new Ray(go.transform.position, transform.TransformPoint(vertices[i]));
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
print("Intersecting " + hit.collider.name);
}
otherwise you have an infinite loop going on because nothing is decreasing i, which causes a crash.
Your answer
Follow this Question
Related Questions
CharacterController with BoxCollider 1 Answer
object passing through walls and floors 1 Answer
2D Object Collision - How To? 2 Answers
Help with Circle Overlap Collision Detection 0 Answers
Box collider fell "inside" into another one (Collision) 3 Answers