0
\$\begingroup\$

I am currently trying to check whether the first object contains a second.

I first tried to use the bounding box, but it is defined in world space, so it is not working with my rotated cubes.

Then I found mesh bounds, but this is not working totally: nothing happens.

Here is my code:

Bounds bounds = GetComponent<MeshFilter>().mesh.bounds;
if (bounds.Contains(target.transform.position))
{
    target.GetComponent<MeshRenderer>().material.color = Color.green;
}
else
{
    target.GetComponent<MeshRenderer>().material.color = Color.white;
}

Then I found one more way, but still no luck:

bool IsInside(GameObject obj1, GameObject obj2)
{
    Vector3 pos = obj1.transform.position;
    pos = obj2.transform.InverseTransformPoint(pos);
    return obj2.GetComponent<Collider>().bounds.Contains(pos);
}
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

You're still mixing and matching different coordinate spaces.

Mesh.bounds is defined in local space. You can't use it to check for containment of a point in world space.

Similarly, Collider.bounds is defined in world space. You can't use it to check for containment of a point in local space.

Be sure to read the documentation and understand what coordinate spaces you're working in to avoid this kind of mix-up.

A corrected version of your code would look like this:

Vector3 worldTarget = target.transform.position;
Vector3 localTarget = transform.InverseTransformPoint(worldTarget);
Bounds localBounds = GetComponent<MeshFilter>().mesh.bounds;

if (localBounds.Contains(localTarget))
{

or

bool IsInside(GameObject obj1, GameObject obj2)
{
    Vector3 worldPos = obj1.transform.position;
    Bounds worldBounds = obj2.GetComponent<Collider>().bounds;
    return worldBounds.Contains(worldPos);
}

Note how using local or world in the variable names helps keep track of the spaces being used, letting you quickly double-check that you're matching up local space points with local space bounds, or world space points with world space bounds.

\$\endgroup\$
4
  • \$\begingroup\$ "You can't use it to check for containment of a point in local space." Same with you code as well \$\endgroup\$ May 12 at 14:10
  • \$\begingroup\$ That's why I'm working with a point in world space, not local space, so I'm not trying to use world space bounds against a point in local space. \$\endgroup\$
    – DMGregory
    May 12 at 14:36
  • \$\begingroup\$ Two questions, the last snippet will work with zero rotation right? Otherwise it wont work. While i checked the first snippet, it is working perfectly with the custom cubes, but if i am trying to use a custom cube (exported from 3d modelling tool) then it no longer work. \$\endgroup\$ May 15 at 7:34
  • \$\begingroup\$ I can't speculate on what's different about your custom cube. Try posting a question with a Minimal Complete Verifiable Example, showing how to create this cube from scratch and how the results you get with it differ from what you expect. \$\endgroup\$
    – DMGregory
    May 15 at 10:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .