0
\$\begingroup\$

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:

enter image description here

Now moving the player with the object connected results in some kind of dragging effect as seen here: enter image description 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.

\$\endgroup\$
  • 1
    \$\begingroup\$ The joint is handling the movement of the object, in an approximation of how a real physical joint would: by applying an off-center torque on the leading object. Give it a try: stick together two LEGO bricks, then push one of them across the table. Left to rotate freely, it will tend to rotate toward the second brick. As it applies forces through their joint (the plastic bumps) to move the second brick along with it, the second brick applies an equal and opposite force back on the lead brick, torquing it around. If you don't want standard physical behaviour, you need to override it yourself. \$\endgroup\$ – DMGregory Jun 2 at 14:27
  • \$\begingroup\$ That totally makes sense! Thank you! \$\endgroup\$ – xeetsh Jun 2 at 15:36
  • \$\begingroup\$ Do you have an explanation for the way to high impulse force when the object connected to the player collides with something? The added weight to the impulse of course increases the impulse force seems to be way higher no matter how heavy the connected object is. \$\endgroup\$ – xeetsh Jun 2 at 15:39
  • 1
    \$\begingroup\$ Have you tried moving with velocity/acceleration/forces rather than MovePosition? It's possible MovePosition says "get to exactly this position in one step, at all costs" and so it ramps up the impulse to an extreme value that exceeds what you'd actually need to travel there smoothly with velocity. \$\endgroup\$ – DMGregory Jun 2 at 15:40
  • \$\begingroup\$ Thanks alot! Setting the velocity direct worked out great! \$\endgroup\$ – xeetsh Jun 2 at 18:13
1
\$\begingroup\$

As DMGregory pointed out the behaviour of the joint connected to the image is totaly right (See his first comment for an explanation).

The problem with the to high impulse force when colliding was solved by changing the velocity instead of using MovePosition like this:

void FixedUpdate() {
    Vector3 movement = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
    movement = movement.normalized * 150f * Time.deltaTime;

    // m_rb is the RigidBody of the player
    m_rb.velocity = movement;

    // m_otherRb is the RigidBody of the connected object
    m_otherRb.velocity = movement;
}

I've just had to turn up the speed multiplyer.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.