Search Unity

Can 2 rigidbodies interact as immovable walls to each other?

Discussion in 'Physics' started by FeastSC2, Oct 31, 2019.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Can 2 rigidbodies interact as immovable walls to each other? (rigidbodies 3D)

    I have a treasure chest and a player. They both have rigidbodies and a collider because I want them to respect the rules of gravity amongst other things.

    However I don't want the treasure chest to be pushed by the player when he comes nearby and touches the treasure chest.

    Here are the options I tried to fix this problem:
    • making the chest a trigger. => that's a problem because then the treasure chest fall right through the ground.
    • set different layers on the player and the chest and disable collisions between the two with the layer collision matrix. => the problem with this is that I want the treasure chest to not overlap with the player, I want the chest to act like it was an unmovable wall.
    • add a new gameobject and a new collider under the rigidbody of the treasure chest. make the rigidbody not interact with the player with the collision matrix and allow the new box collider to interact with the player. => This doesn't work because it seems that rigidbodies do not allow/function with 2 different layers. The additional box collider acts like it's on the same layer as the rigidbody.
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    If I've understood you correctly, you may try these one of these options:
    • Remove the rigidbody from the chest. This leaves the chest colliders only, which make it unmovable.
    • Mark the chest rigidbody as kinematic. This would have the same effect as the previous choice.
    • Configure a large mass in the Rigidbody. Thus, the player may touch it but not be able to move it.
     
    SparrowGS likes this.
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You can try to make the chest extremely heavy, like neutron-star dense.
    A hacky solution, but a solution.
     
  4. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I did set the mass very high previously on the treasure chest, a mass of 1000. And you guys are right, the player with a small mass of 1 won't be able to move it (it actually moves a little bit but so few that one could consider it ok).

    The problem with this came the moment I created other objects with a high mass that could move. If this object touches the treasure chest it will get pushed because they both have a mass of 1000. Basically this solution has downsides but it might be ok for some games, just not mine.

    I need a solution where the object interacts like a wall with the other rigidbodies. Any other ideas?
     
    Last edited: Nov 1, 2019
  5. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    • Remove the rigidbody from the chest. This leaves the chest colliders only, which make it unmovable.
    • Mark the chest rigidbody as kinematic. This would have the same effect as the previous choice.
     
  6. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I don't understand what you're saying:
    1) remove the rigibody
    2) mark the rigibody as kinematic. If I removed the rigidbody in step1, is that 2nd rigidbody?

    I need the treasure chest to be able to use gravity and fall when there's no ground below it so being kinematic might pose a problem.
     
  7. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Oh, ok. I had assumed that you wanted to keep it always static. Please disregard my last post.
     
  8. DiamondWolf2

    DiamondWolf2

    Joined:
    Dec 9, 2018
    Posts:
    16
    Doesn't zero mass equate to infinite mass as far as Unity is concerned?
     
  9. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I ended up having the objects that are movable at a mass of 100, the objects that can't be moved at a mass of 10000 and the player at a mass 1.
    Basically having big enough differences in mass so that these object do not mess with each other. That doesn't seem like a super solid way though. I'll use that for now but I hope to find another better way.


    No, it means it's really easy to move when I tested.
     
  10. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Zero mass is not allowed indeed. It would cause NaNs along the engine. If you specify zero (or negative) then the minimum supported mass is used (this minimum seems to be 1e-07, as per the limit in the Rigidbody inspector).
     
  11. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I found a good reason to not use mass to stop physics object from moving each other.

    Here's the situation:
    I want my player to not be able to push boxes through the physics, the box has to stay immobile when the player collides with it.
    What I do instead: I make the player press a button to enter "Push mode" and then I set the rigidbody.velocity of the player and the box to a certain value.

    In order for the player not to move the pushable through physics I had to put a mass high mass on the pushable. Player has 1 mass, Pushable has 1000 mass.

    However now I understand that while having a high mass fixes the problem of the player not being able to move the pushable, it creates another problem:
    -> The pushable has so much mass that the reaction force given by the player to the pushable is too weak for him not to get squashed between a wall and the pushable.


    That's the behaviour with the pushable = 1 mass and player = 1 mass. NO PROBLEM:
    https://i.imgur.com/XbEaxO2.gifv

    That's the behaviour with the pushable = 1000 mass and player = 1 mass. PLAYER SQUASHED:
    https://i.imgur.com/fDapad9.gifv

    How to make certain physics objects not push other physics object (without modifying their mass) and have them interact as wall to each other?