- Home /
Why is my Rigidbody child not moving with it's Rigidbody parent?
I'm sure I've solved this problem before with characters on moving platforms, but nonetheless I'm getting behaviour that's confusing me now.
I have two cubes stacked on top of each other, each with a box collider and rigidbody.
The upper cube is the child of the lower cube. Gravity on both rigidbodies is disabled.
In editor and play mode, moving the parent cube by dragging the transform widget moves the child cube (it inherits the transformation).
However, when I run the following simple test code on the parent (moving it with movePosition), the child cube remains motionless, failing to inherit the transformation of the parent.
private Rigidbody rb;
public float percent = 0.0f;
void Awake()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
percent += Time.deltaTime;
rb.MovePosition(Vector3.MoveTowards(Vector3.up * 2 + Vector3.left * 4, Vector3.up * 2 + Vector3.right * 4, percent));
}
As I say, I'm certain I've used something similar for characters and moving platforms before (parenting onCollisionEnter etc.)
What's the obvious thing that I'm missing here? This shouldn't be confusing me like it is...!
Answer by SlowCircuit · Sep 26, 2020 at 05:20 AM
Rigidbodies do not inherit forces/velocity from parent rigidbodies. They act independently. You could apply the move position to both, but there's always the possibility that the child will hit the parent when trying to do that. You could use FixedJoint to keep them together (might need to unchild it), but it depends on what you're trying to accomplish. FixedJoint would really keep them together, but if you want the child to also be able to move on it's own but move with the parent as well, then you might want to take a look at other joint types and see what works best for what you want.
Hi @SlowCircuit ! That seems to contradict the docs, though, no?
Parenting When an object is under physics control, it moves semi-independently of the way its transform parents move. If you move any parents, they will pull the Rigidbody child along with them. However, the Rigidbodies will still fall down due to gravity and react to collision events.
Not sure FixedJoint is going to help much as I want the child to move horizontally independently of the parent. So odd, though, I could have sworn I had similar behaviour working with a character standing on a moving platform...
Ach, to confess, I thought this would be a straightforward issue and it was getting late, so I just posted and hit the hay. =) I'll do some more research here. Thanks for your time!
The doc is confusingly worded. I believe it's saying if you move the parent's transform it will move the child's transform as well, but moving the parent rigidbody will not move the child's rigidbody. You can google "unity parent rigidbody not moving child" and you'll find a lot of people with similar issues to you who also have a rigidbody on the child. For what you describe I'd do my first suggestion. Keep it childed. On the parent, assign the child rigidbody in the inspector. Then when you add forces to the parent, add them to the child too. It should work well for what you're trying to accomplish.
You are correct, rigidbodies always act independently. Here's a post from Edy's vehicle physics: https://forum.unity.com/threads/child-rigidbody-doesnt-act-normally-when-parent-rigidbody-moves.564991/#post-3754306