Physics.ComputePenetration doesn't work with 2 box colliders
So I have a fairly simple setup. I have 2 meshes, with a box collider wrapped around each. In my actual implementation (I am making this simplified to make this post easier to understand) I use convex mesh colliders on each of the objects.
I have a bunch of pipes in my level, and I want to make a graph, holding all of the connections. I would love for a better way to do this, but my algorithm for now is simple:
Use
Physics.OverlapBox()
to find all of the nearby collidersFor each of the nearby colliders, use
Physics.ComputePenetration()
to determine if I am actually colliding with the nearby colliderIf I am colliding with it, add that to my list of connections
Below is the sample code, and a screen shot of a simplified example. 2 objects with BoxColliders. My script isn't picking up on the collision. The Physics.OverlapBox()
call finds the nearby collider, but for some reason Physics.ComputePenetration()
doesn't think they are overlapping.
void Update()
{
SimpleCollisionTest();
}
void SimpleCollisionTest()
{
Collider myCollider = gameObject.GetComponent<Collider>();
// Get all of the nearby colliders
// This finds the other cube
Collider[] overlaps = Physics.OverlapBox(myCollider.bounds.center, myCollider.bounds.extents);
// For each of those colliders nearby, see if I'm actually colliding with them
foreach (Collider overlapCollider in overlaps)
{
bool isOverlapping = Physics.ComputePenetration(
myCollider, myCollider.transform.position, myCollider.transform.rotation,
overlapCollider, overlapCollider.transform.position, overlapCollider.transform.rotation,
out Vector3 direction, out float distance
);
// If we are overlapping something, and it isn't ourselves, simply log it
if (isOverlapping && overlapCollider.gameObject != myCollider.gameObject)
{
Debug.Log($"found overlap between {myCollider.gameObject.name} and {overlapCollider.gameObject.name}");
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613044903im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I need the functionality of both OnTriggerEnter and OnCollisionEnter in unity 1 Answer
How to make gridbased movement while also using collision? 0 Answers
Is there a way to limit animation on collision? 1 Answer
Why Does Bounciness Affects Rigidbody Velocity? (SOLVED) 0 Answers
Why/How 2d tower of blocks collapse? 0 Answers