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

Is it possible to test if an object is inside another?

Discussion in 'Scripting' started by Lupus, Jun 25, 2014.

  1. Lupus

    Lupus

    Joined:
    Nov 6, 2013
    Posts:
    25
    Hello,
    My map will have areas that I will use and need to separate. I was thinking in making the areas as invisible/transparent cubes, but I need a way to tell if the player is inside one of this areas and witch area. Is there a way to test if an object is inside this cube-area? I needed to make this test from time to time and want to make this test inside a function called by an InvokerRepeating. Is it possible? How?
    Thank you.
     
  2. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    Code (csharp):
    1. collider.bounds.Contains(Vector3.one);
     
    Lupus likes this.
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    There aren't direct methods to test if something is inside a collider.

    You can test in the bounds, but that's not the actual shape of the collider.

    I personally wrote my own geom wrappers that define the math of all the known collider types (except mesh, for obvious reasons). And have 'overlap' and 'contains' methods for each. Geometry like cubes are pretty easy to test (dterbeest's example is pretty much it), sphere is super easy (you just test if something is in the range of the radius), and capsules are really just 2 spheres.
     
  4. Lupus

    Lupus

    Joined:
    Nov 6, 2013
    Posts:
    25
    It worked, thank you.