Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.

    Read our full announcement for more information and let us know if you have any questions.

How to find all the bodies that the given one is touching?

Discussion in 'Physics' started by alexzzzz, Jan 20, 2015.

  1. alexzzzz


    Nov 20, 2010
    There are a couple of rigidbodies touching each other and/or the static ground. All the bodies are, most likely, sleeping. The problem I'm trying to solve is the following:

    There's an object the player wants to move, let's call it a platform, and there may be other objects touching the platform. Those that touch only the platform should be moved with it like children (the pair of cubes on the picture). Those that also touch something else (like the plank that touches the platform and the ground) shouldn't be moved with the platform.


    OnCollisionStay() has all the information I need, but sleeping bodies don't receive such a message.
    In order to get this message for the platform I have to
    1) wake up all the bodies around it,
    2) prevent them from sleeping for a while,
    3) shake them a little to generate new collisions (because it looks like the old collision information gets lost when the body falls asleep).

    This doesn't look like a sound solution at all.

    The objects may be simple boxes or convex meshes or complex compound bodies with dozens of parts.

    Rigidbody.SweepTestAll doesn't return bodies that are touching the current one already. Besides, it doesn't support convex mesh colliders.
    Last edited: Jan 20, 2015
  2. Cpt Chuckles

    Cpt Chuckles

    Dec 31, 2012
    i've never done this before but you might have to look into creating physics materials for them and adding friction coefficients, that might do it
  3. alexzzzz


    Nov 20, 2010
    When the player moves the platform it may rotate it as well, and if he does the child objects shouldn't fall from it (at least until the player releases the platform).

    I spent some time studying how OnCollisionStay works. I'm still not sure but it looks like if a body is sleeping and doesn't have OnCollisionStay event handler, it won't receive these messages even if I attach a script with such a handler in the middle of the sleep. But if the body has OnCollisionStay handler at the moment of OnCollisionEnter, then it will continue to receive OnCollisionStay messages event if the body falls asleep.
  4. ExtraAmmo


    Mar 12, 2015
    Assuming that the bodies collide at some point before you go back to move the platform, you should cache the collision info. Maybe a Who-I'm-Touching component that updates with the collision events and makes the contact info available when needed.