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 get all gameObjects that is currently touching another gameObject without triggers.

Discussion in 'Scripting' started by Acer85, May 24, 2021.

  1. Acer85

    Acer85

    Joined:
    Mar 10, 2019
    Posts:
    28
    So I want to call a method from a script on the gameObject to see all the gameObjects that are currently touching/colliding with it.
    I can store all the gameObjects with onTriggerEnter/exit but it does not feel lite the best way.

    I have thousands of gameObjects like this so I do not want them to have there own logic.
    So if a do this I only have to check one gameObject and I'am only itrested in that gameObject. Imagine you're clicking on the gameObject an then the script checks the touching/colliding gameObjects.

    Is there a way to do this?
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,194
    If this is in 2D, you can use Collider2D.Cast to "Casts the Collider shape into the Scene starting at the Collider position ignoring the Collider itself."

    There's no such method in 3D physics. If the collider has a known shape, you can use one of the overlap methods to check for overlaps, and then manually filter out the target collider. See Physics.OverlapBox for an example.
     
    Acer85 likes this.
  3. Acer85

    Acer85

    Joined:
    Mar 10, 2019
    Posts:
    28
    I work in 3D and thank you for the answer this seems to work fine =) have to check a little bit more.
    [Update] It works perfect with OverlapBox! :)
     
    Last edited: May 24, 2021