0
\$\begingroup\$

I'm creating a level editor in Unity. I need to detect when an object is intersecting another as I don't want to allow the user to place an object when it is colliding with something else.

In the below diagam, there is a corner road piece and a tree. The dashed red line represents the mesh bounds of the road and the blue dashed line represents the mesh bounds of the tree.

enter image description here

My current approach is to use Physics.OverlapBox with mesh.bounds. In the diagram, visually, it is clear that the tree does not overlap the road. However, the bounds do overlap, therefore I cannot place the tree there.

Whats the best way to check if they are actually colliding in an efficient way? The level editor could potentially have thousands of objects.

EDIT: I have since tried using Physics.ComputePenetration. Primitive-primitive checks work ok, primitive-mesh collider checks work ok, but mesh collider-mesh collider checks fail. From what I understand a concave mesh collider cannot interact with another concave mesh collider. Is there a workaround for this or must I momentarily make it convex before checking overlap? I just don't want to lose the accuracy of the concave collider.

\$\endgroup\$
  • \$\begingroup\$ Tests with concave colliders are vastly more expensive than convex ones, so it's usually a good idea to decompose your concave colliders into multiple convex or primitive pieces wherever possible. \$\endgroup\$ – DMGregory Nov 28 '18 at 18:23
0
\$\begingroup\$

To solve this I first use Physics.OverlapSphere to get the nearby colldiers. Then for a more precise calculation I use Physics.ComputePenetration.

However, some of my assets utilize concave mesh colliders. As stated in the docs, a concave mesh collider cannot collide with another concave mesh collider. To fix this, I momentarily make the mesh collider concave before calling the overlap sphere.

\$\endgroup\$
  • \$\begingroup\$ If your issue is solved, could you mark your answer as correct, to prevent it from being bumped to the homepage? \$\endgroup\$ – TomTsagk Jan 8 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.