I have 2 cubes connected to an Fixed Joint. Now I want to move one cube away from the other cube. (make the distance between the cubes bigger).
Is there a better solution than kill the fixed joint, change the distance and then create a new fixed joint?
Maybe you could try changing from using a fixed joint to a configurable joint instead?
Configurable Joints are extremely customizable. They expose all joint-related properties of PhysX, so they are capable of creating behaviors similar to all other joint types.
Turns out any attempt to move the hinged object using transform.position will be snapped back to where it started. you can’t disable the hinge, but you can unscrew the far end:
// Save end of hinge, then unscrew it:
Rigidbody RB = transform.hingeJoint.connectedBody;
transform.hingeJoint.connectedBody = null;
// Tricky, since anchor is local. No easy way to move it "world backwards":
//transform.hingeJoint.anchor += Vector3.forward * 0.1f;
// Can now move unconnected hinge:
transform.position -= Vector3.forward*0.2f;
transform.hingeJoint.connectedBody = RB; // screw it back in
Use configurable joint, linearLimit
ConfigurableJoint configurableJointComp = GetComponent();
SoftJointLimit softJointLimit = new SoftJointLimit();
softJointLimit.damper = 1f;
configurableJointComp.linearLimit = softJointLimit;
In addition to the above, defining the X-drive, y-drive, and z-drive in conjunction with a target position/velocity can be used to move the object to specific distances, creating a “length” to the joint arm. Be sure to set the x/y/z-drive spring, as this is what pushes the object out to the limit.
If it’s 2D game, use Distance Joint 2D. You can write something like:
joint.distance = yourDistance;
or set EnableCollision and Max Distance Only for minimum distance.