Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

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

  1. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    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.

    Untitled-1.jpg

    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.

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

    PPS
    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

    Joined:
    Dec 31, 2012
    Posts:
    86
    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

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    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

    ExtraAmmo

    Joined:
    Mar 12, 2015
    Posts:
    11
    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.