Search Unity

Adding collider runtime moves body

Discussion in 'Scripting' started by Abatuf, Apr 3, 2015.

  1. Abatuf

    Abatuf

    Joined:
    Jan 9, 2014
    Posts:
    18
    Hi there!
    I am trying to make some physics additions to my project and encountered some weird behavior of RigidBody:
    I am making first GameObject, this one has RigidBody(without gravity, with some mass).
    Then i am adding some GameObjects as childs. Everything ok there.
    Then i am adding colliders to those GameObjects(its cubes so cube collider).

    And there is strange thing - the more i add colliders(to those childs) - the more parent GameObject moves from it original position. By little, but moves.

    Childs added with .SetParent (transform,false); and set position with localPosition.
    What am i doing wrong?
     
  2. zehreken

    zehreken

    Joined:
    Jun 29, 2009
    Posts:
    112
    As far as I know, it is the collider which defines the center of mass of an object with rigidbody. So when you change the collision shape of an object by adding new colliders, you also change the center of mass of that object. Try adding the rigidbody after you are done with children and their colliders.
     
  3. Abatuf

    Abatuf

    Joined:
    Jan 9, 2014
    Posts:
    18
    Yes! Actually i am adding and destroying colliders all time in runtime so managing centerOfMass manually solves this flawlessly, thanks!