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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Simple rigidbody2D physic question on interaction between 2 objects

Discussion in '2D' started by pbeau, Jul 20, 2015.

  1. pbeau

    pbeau

    Joined:
    May 26, 2014
    Posts:
    3
    Hi,
    I have two game objects on top of each other in a 2D environment. The one on top has a Rigidbody2D and a Collider2D and the one under has a Collider2D. When running the scene, the object on top falls with gravity and sits on the object below, which is correct. Then I move the object under along the x axis and only this one moves. The object on top slides and remains in position on the x axis. Why isn’t the object on top moving along with the object below? Shouldn’t simple physic tell it to move along with the moving object? It works on the Y-axis but not the x-axis. I am using Unity 5. Thanks


    screen1.png
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    But if you don't have a RigidBody2D om the object at the bottom, how are you moving it? If you are using translate or change the position, you are not using the physics for the movement.

    However, if you add a RigidBody2D to both objects and move the bottom one using forces or manipulate the velocity, then it should work properly.
     
  3. pbeau

    pbeau

    Joined:
    May 26, 2014
    Posts:
    3
    Thanks for your reply PGJ but if I put Rigidbody on the bottom gameObject it will fall with gravity, and if I put it as Kinematic, it won't respond to Force? Am I right? I am trying to make a moving platform, but how do I get the platform to move and carry the object sitting on it? I have been moving the bottom gameObject using iTweet.MoveTo function.

    Thanks again
     
  4. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    There are several way to get around this.

    Set it kinematic and move it either using RigidBody2D.MovePosition or by setting its velocity. I know that there was penelties for moving rigidbodies in older version of Unity, not sure if that still holds true.

    You could also set Gravity Scale to 0 and give the object a huge mass, so that it won't be affected by the object on top.

    Use a slider joint to keep it in place.

    If you are on 5.1 you can use Constraints to Freeze Position Y (you probably want to Freeze Rotation as well).

    There are probably other ways as well, but that should get you started.
     
    theANMATOR2b and pbeau like this.
  5. pbeau

    pbeau

    Joined:
    May 26, 2014
    Posts:
    3
    Thank you, will give this a try