0
\$\begingroup\$

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

Ball and hinge

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;
New contributor
HN Learner is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
  • 1
    \$\begingroup\$ How have you tried changing the distance so far? Has that caused errors or undersired behaviour? \$\endgroup\$ – DMGregory Jan 10 at 12:34
  • \$\begingroup\$ I have used this code, by removing previous joint and adding a new joint of same characteristics with a little change in Y-axis of joint.anchor. if(joint) { Destroy(joint); joint = gameObject.AddComponent<ConfigurableJoint>(); joint.anchor = transform.transform.InverseTransformPoint(closestHinge.position.x,closestHinge.position.y+1,closestHinge.position.z); joint.xMotion = ConfigurableJointMotion.Locked; joint.yMotion = ConfigurableJointMotion.Limited; joint.zMotion = ConfigurableJointMotion.Locked; } \$\endgroup\$ – HN Learner Jan 10 at 23:59
  • \$\begingroup\$ You probably noticed that code doesn't look very readable in comments. So, don't put it there. Edit your question to ensure it contains all the information you want us to know to be able to help you. That way it's all in one place and you can format it legibly. \$\endgroup\$ – DMGregory Jan 11 at 0:05
0
\$\begingroup\$

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:
  var joint = hingeObject.AddComponent<ConfigurableJoint>();
  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);
New contributor
real4x is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$

Your Answer

HN Learner is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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