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\$
5
  • \$\begingroup\$ "You can't use it to check for containment of a point in local space." Same with you code as well \$\endgroup\$ Commented May 12, 2023 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\$ Commented May 12, 2023 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\$ Commented May 15, 2023 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\$ Commented May 15, 2023 at 10:47
  • \$\begingroup\$ Move from local to world then check. You have assumed consistency of the local space from possible disparate sources(An unknown static maybe). transform.InverseTransformPoint is quite slow, if possible at all, since not matrices are invertible. The forward transform of all positions into world coordinates is much faster. \$\endgroup\$ Commented Oct 26, 2024 at 22:54

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.