What is the best way to move an object with other objects connected to it via joints?
Let's assume we have the following setup:
- A player that can move on the x and the z axis
- "Normally configured" RigidBody
- Moving like this:
void FixedUpdate() {
Vector3 movement = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
movement = movement.normalized * 2f * Time.deltaTime;
// m_rb is the RigidBody of the player
m_rb.MovePosition(transform.position + movement);
}
- Another object that should be moving with the player as it were its child
- Connected to the player via a fixed joint
- "Normally configured" RigidBody
When moving the player without any object connected the movement looks as expected:
Now moving the player with the object connected results in some kind of dragging effect as seen here:
One way to solve this behaviour would be to also move the connected object:
// m_otherRb is the RigidBody of the connected object (m_other)
m_otherRb.MovePosition(m_other.transform.position + movement);
Is there a better way to solve this? Shouldn't the joint handle the movement of the other object? Moving the connected object like this results in many problems like to high impulse forces when colliding with something as both elements get pushed towards the collider. Also handling the rotation of the connected object isn't as easy as the position.