# Decreasing distance for a Configurable Joint at run-time | Unity

I have written a script that uses ConfigurableJoint to connect a ball to a hing so that it moves in a fixed circular path around the hinge. The problem is that I want to decrease the distance between hinge and ball with time. In other words, I wamt to reduce the radius of circular path on which the ball is moving.

I have created a rough image to demonstrate what I'm trying to do

And below is the code that I am using to create the joint:

     joint = gameObject.AddComponent<ConfigurableJoint>();

joint.anchor = transform.transform.InverseTransformPoint(closestHinge.position);

joint.xMotion = ConfigurableJointMotion.Locked;
joint.yMotion = ConfigurableJointMotion.Limited;
joint.zMotion = ConfigurableJointMotion.Locked;

• How have you tried changing the distance so far? Has that caused errors or undersired behaviour? – DMGregory Jan 10 '19 at 12:34

You need to adjust the anchor(s). Note, that they are defined in the local coordinates, relative to the owner object. There are two anchors: ConfigurableJoint.anchor and ConfigurableJoint.connectedAnchor. You can adjust either one, but if you create the bodies in autoConfigureConnectedAnchor mode, then it may be challenging to figure out how to adjust which anchor.

I'd suggest to no rely on the automatic behavior and set both anchors manually when creating the joint. This way you'll know exactly how and what to adjust.

Setup the joint like this:

  // Given:
GameObject hingeObject;
GameObject ballObject;

// Then:
joint.autoConfigureConnectedAnchor = false;
ballObject.transform.LookAt(hingeObject.transform);  // Optional.
joint.connectedBody = ballObject.GetComponent<Rigidbody>();
joint.anchor = Vector3.zero;
var initDistance = Vector3.Distance(hindgeObject.transform.position,  ballObject.transform.position);
// ATTENTION! It's important to know what axis your joint uses!
// If doesn't work, try to move initDistance to X or Y.
joint.connectedAnchor = new Vector3(0, 0, initDistance);


Now changing the distance is trivial:

  // Don't forget about axis.
joint.connectedAnchor = new Vector3(0, 0, newDistance);