Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to detect if an object is inside another object without using collision triggers?

Discussion in 'Scripting' started by toreau, Aug 3, 2014.

  1. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    Hi!

    I need to find out if an object is inside another object. This seems easy, and after a lot of googling around there are many answers to the "problem." However, all of the answers has to do with checking the objects involved's OnCollision trigger, and in my example I'm unable to do that.

    So far my approach has been to create a list of possible Vector3s based upon the main object's renderer-extents, but this seems like overkill as I have to create many Vector3s based on min/max x, y, z etc. and check if they are Contain'ed by any of the nearby objects.

    Is there a better - and easier - way?

    Thanks!
     
  2. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    For a rough check you can use check if the inner objects bounding box (bounds) is within the outer objects bounding box.
    Note this would not be mesh accurate.
     
  3. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    Uhm. That's what I already do, but it is a lot of work to check Vector3s for all the min/max extents of the bounding box. Is there an easier way?
     
  4. S-dieters

    S-dieters

    Joined:
    Aug 27, 2013
    Posts:
    42
    if you are sure you dont want to use collision models, i would go for the world location of both objects.
    but again, this will not be very accurate, depending on the meshes
     
  5. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Yep you could do a distance check and use the radius of the objects bounding box (might need to calculate this).