Search Unity

Stacked RigidBody2Ds don't shift when bottom one is moved

Discussion in 'Physics' started by Mr-Men, Oct 14, 2018.

  1. Mr-Men

    Mr-Men

    Joined:
    Feb 18, 2014
    Posts:
    16
    I've got 2 boxes (2D, so squares), each with a RigidBody2D and a BoxCollider2D. Both have a physics material that has a friction of 1, and a bounce of 0. Both have a mass of 1, gravity scale of 1 and linear and angular drag of 0.5.

    When I stack them, and click and drag the bottom box side to side, the top box doesn't move. It's as if there's no friction at all between the 2 GameObjects. I've played round with the RigidBody2D's drag values and the friction value of the Physics material, but there's no change in behaviour.

    I don't want to create a joint between the two, because it's possible to click and drag the top box, in which case the bottom box should move independently.

    Does anyone have any idea on what's causing the top box to not move, or how to overcome it?
     
  2. jvggp

    jvggp

    Joined:
    Jul 8, 2018
    Posts:
    30
    If you move in the Editor so the #scene window it doesn't work.
    Move it by script.
     
  3. Mr-Men

    Mr-Men

    Joined:
    Feb 18, 2014
    Posts:
    16
    I'm not clear on what you mean. This behaviour occurs when I'm running the game from an executable. The click and drag is via a script.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    Sounds like your "dragging" isn't actually dragging, it's just repositioning the bodies i.e. teleporting them from position to position. If that's the case then you shouldn't be surprised there's no friction. For friction to apply the body has to be moving through space. Using Rigidbody2D.MovePosition or adding forces, setting velocity is the way to go.
     
  5. Mr-Men

    Mr-Men

    Joined:
    Feb 18, 2014
    Posts:
    16
    @MelvMay, thanks for your response. I've been away from the computer and haven't had a chance to read your reply any earlier.

    I'm careful not to move GameObjects by setting their transform.position. Instead, I move the 2D box by creating a SpringJoint2D and setting the Anchor to the point in the box where the click occurred. The ConnectedAnchor is continuously updated to the current position of the mouse.

    Should this cause the friction to be applied to the stacked boxes? If not, then I'll rework the drag technique to us MovePosition.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    That is perfectly fine then as it's an internal force being applied. Note that maybe you should consider using a TargetJoint2D instead like this.

    Yes but to ensure the stacked boxes move, the friction forces need to be high enough.

    Note you can see an example of how dragging can be done in my example GitHub project here, specifically this scene.
     
  7. Mr-Men

    Mr-Men

    Joined:
    Feb 18, 2014
    Posts:
    16
    Thanks for pointing out TargetJoint2D. I didn't realise it existed and it suits my requirements perfectly.

    I'll play round with the mass of the stacked boxes, to see if that makes a difference.