Search Unity

MovePosition in parent/child relations of rigidbodies

Discussion in 'Scripting' started by simonal, Jun 22, 2009.

  1. simonal

    simonal

    Joined:
    Apr 17, 2009
    Posts:
    16
    I am new to Unity and have a question about moving kinematic rigidbodies.

    Say you have two kinematic rigidbodies rb1 and rb2 where rb1 is parent to rb2. Now, if rb1.MovePosition() is called, both objects will move there global positions, but a simple test shows that only rb1 is physically updated (PhysX moveGlobalPosition). Rb2 seems to be updated with PhysX setGlobalPosition.

    What is the recommended way to move rb1 and still get rb2 updated in a physical way?
     
  2. Risine

    Risine

    Joined:
    Dec 10, 2009
    Posts:
    154
    I have the same problem, when applying the moveRotation to my rigidbody, children rigidbodies are not updated.
    Does someone have an idea?
     
  3. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Having one rigidbody as the child of another is generally not a good idea, indeed the manual specifically warns against it. If you have a rigidbody on the parent, collisions on the child's colliders will be reported to the parent's script. Also, you can change the parent's centerOfMass property to create the same effect as having additional mass on the child object.
     
  4. Risine

    Risine

    Joined:
    Dec 10, 2009
    Posts:
    154
    If you are using joints attached to the parent rigidbody, children rigidbodies are created automatically for each joint.
    In this case, shouldn't a MoveRotation applied to the parent rigidbody take in charge all the children as well?

    Any idea on what to do with children rigidbodies to make them follow the parent rigibody?
     
  5. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    If you move the parent and it has children connected with joints (like a ragdoll), then you would normally want the child objects to move independently. The only way I can think of to reduce this is to set limits on the joints' movement and also possibly add springs to make them return to a neutral position.
     
  6. FlyingJoe

    FlyingJoe

    Joined:
    Sep 26, 2010
    Posts:
    8
    I'm relatively new to Unity and I'm very confused by how parent objects move their children. It doesn't appear to be consistent. I have two serious issues:

    First, I have a parent object that is a kinetic rigidbody. It has a child that is not a rigidbody but does have a collider. When I call 'MovePosition' on the parent, the child object appears to move with the parent but the collider doesn't - or in some cases it moves but a RayCast does not actually detect a hit to the collider.

    gameObject.rigidbody.MovePosition(finalSpot);

    I can get the collider back to working and fix everything if I move the object via it's transform. In other words if I just call:

    gameObject.transform.position = finalSpot;

    Shouldn't these two result in the object in the same place with all it's attendant colliders etc?

    The second situation is even weirder. Take the same parent rigidbody, but this time add a child that is rotated more or less 180 degrees in all directions. So now the parent and child object have rotations that are more or less inverted. This time when I call the same method gameObject.rigidbody.MovePosition(finalSpot); the object actually goes all over the place. If I take the child off the parent the object goes back to moving correctly. Put the child on and whamo it behaves totally wrong.

    Is this a bug or am I missing something?
     
  7. kaedmon

    kaedmon

    Joined:
    Jan 28, 2010
    Posts:
    25
  8. kaedmon

    kaedmon

    Joined:
    Jan 28, 2010
    Posts:
    25
    Last edited: Nov 17, 2010
  9. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,632
    I ran into the same problem trying to use multiple colliders parented to a game object with a rigid body, except mine was not creation within a script, rather just starting the scene with this hierarchy.

    Motive Collider (capsule collider + rigidbody)
    |
    |__ Hit Collider (capsule collider + kinematic rigidbody)

    My topmost game object w/ rb is being used for a motive collider for an enemy (collisions with background). I have a 2nd game object underneath the main one which contained another capsule collider + kinematic rigidbody -- this one was being used as a bullet hit detector. I move the topmost (motive) collider by rigidbody.MovePosition(newPosition) when the 2nd child hit collider collides with a bullet. (Bullet does not collide with motive collider.) After a succession of bullet hits, the main game object would move across the field, but the hit box which the bullets were colliding with would remain in the original location (invisible child collider at original location, topmost motive collider and game object at new position).

    The result was baffling. The collider gizmo and Debug.Log position info on the child transform reported they were in the same location as the topmost game object and collider, but it clearly wasn't the case. I did some tests and found the child rigid body was sleeping and no amount of collisions or even .WakeUp() would ever wake it! Even weirder, I found the ONE thing that would wake this rigid body was applying a transform.rotation = to the topmost game object. So as long as I was inputing a rotation to the main transform every frame (even if the rotation was the same each frame), the child rigid body would be awake and everything would work as desired. Stop the rotation = and the child rb falls asleep.

    So after wasting the better part of a day on this bug, I made a compromise and removed the rigid body on the child collider (hit collider). I was already doing a bunch of Physics.IgnoreCollision() calls managing what can hit what. Interestingly, the parent RigidBody extends down to include the child collider, and with my ignorecollisions, I get the result I was looking for in the first place without having two rigid bodies. The only issue is when detecting a collision with the hit collider, the bullets report they're hitting the topmost gameobject, not the child gameobject.

    I hope this helps someone.