2
\$\begingroup\$

I have a script on my player to move. I do not have root motion applied because, root motion does not work perfectly so I'm manually adding velocity to the player.

The player is holding a ball and the ball is a child of the player. However, when I apply force to the player it seems to apply force to the ball so he stops holding it and it is just floating around instead.

My player and my ball both have rigidBodies and I'm applying force like this.

    var velocityX = horizontal * HorizontalSpeed * Time.deltaTime;
    var velocityZ = vertical * VerticalSpeed * Time.deltaTime;


    rigidBody.velocity = new Vector3(velocityX, 0f, velocityZ);

I know as a hack I can apply a late update to the ball, but I'd rather understand what is going on.

Can anyone help me figure this out?

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

When we parent a game object to another game object, we make the Transform values of the child object local to the Transform values of the parent object.

This is very useful, if used correctly. In the above situation, we have a character holding a ball. While we move the character with the assumption of still holding on to the ball, we can simply move the character. As it is local to the character, the ball moves with it. However, this does not work when we parent separate objects containing Rigidbody components. A Rigidbody interprets every child object as part of that Rigidbody, where a child Rigidbody can break this assumption, and lead to odd behaviour.

As detailed at Unity Answers, you should use joints to attach Rigidbody game objects.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks is that somewhere in the documentation, can you provide a link. Also, do you know of a way to fix this or temporarily disable the rigid body component? \$\endgroup\$
    – johnny 5
    Commented Feb 2, 2017 at 2:19
  • \$\begingroup\$ Hey I tried adding a fixed joint but I'm having some issues. I've posted a new question if you have time to help me out I would be very grateful here is the link gamedev.stackexchange.com/questions/136785/… \$\endgroup\$
    – johnny 5
    Commented Feb 3, 2017 at 21:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .