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. Dismiss Notice

Rigidbody2D bug?

Discussion in 'Editor & General Support' started by keht, Oct 4, 2014.

  1. keht

    keht

    Joined:
    Oct 4, 2014
    Posts:
    6
    [I use Unity 4.5.2f1]

    0. Add two GOs to the new scene.
    1. Add to both GOs RigidBody2D component and set Mass = 2, GravityScale = 0.
    2. Add script to the first GO:
    Code (CSharp):
    1.   void FixedUpdate()
    2.   {
    3.       Rigidbody2D body = GetComponent<Rigidbody2D>();
    4.       body.AddForce(new Vector2(0, -9.81f), ForceMode2D.Force);
    5.   }
    3. Add script to the second GO:
    Code (CSharp):
    1.   void FixedUpdate()
    2.   {
    3.       Rigidbody2D body = GetComponent<Rigidbody2D>();
    4.       body.mass = 2;
    5.       body.AddForce(new Vector2(0, -9.81f), ForceMode2D.Force);
    6.   }
    4. Run scene.

    First GO will fall faster than second. Why??
     
  2. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,490
    Because it takes more force to get the body of mass 2 to start moving from a state of zero movement.
     
    Last edited: Oct 5, 2014
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    If they both start with a mass of 2 like you said, they fall at the same speed. If you don't see that, it's because the mass of the first one is different.

    --Eric
     
  4. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,490
    I kind of disregarded that as a typo assuming the first one was left with the default mass of 1. So just to be sure that setting the mass = 2 each frame wasn't causing a bug, I tested it. They both move at the same speed if you set the mass to 2 in the inspector on both RB2D's. If you leave the first one with a mass of 1 like I assumed he did in the first place, then yes, the first falls faster because of the reason I posted above.
     
  5. keht

    keht

    Joined:
    Oct 4, 2014
    Posts:
    6
  6. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,490
    Strange. Update to 4.5.4 and try it. I did my tests in that version and didn't see that behavior.
     
  7. keht

    keht

    Joined:
    Oct 4, 2014
    Posts:
    6
    After update all works as expected. It really was a bug in the old Unity version. Thanks for help!
     
  8. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,490
    Great!