- Home /
 
How to get Joint connectedBody OnJointBreak
I have multiple joints connected to a group of objects. If a joint breaks I want the original object and the connected object to respond. The method OnJointBreak doesn't give me any indication of the Joint connectedBody. How can I find the body that was connected?
Why the heck would they only give break Force!? Please help.
Answer by prak · May 09, 2013 at 04:07 PM
I created a work around that is not what i want, but until someone can suggest something better this is what i did.
created an array of connections
 //joints
 public ArrayList<Part> connections;
 
               then as i create joints i add them to both parts, the original and the connectedBody.
 parts[i, j].connections.Add(parts[i + 1, j]);
 parts[i + 1, j].connections.Add(parts[i, j]);
 
               then when a joint breaks i flip a flag and wait:
 void OnJointBreak(float breakForce) {
     JointBroken = true;
 }
 
               in the next update (when the joint has been destroyed) I the go looking for the missing joint:
 if (JointBroken) {
     Ship.EvaluateJointBreak(this);
     JointBroken = false;
 }
 
               then i can find and act on missing joint
     public static void EvaluateJointBreak(Part part) {
         foreach (Part sp in part.connections) {
             if (sp == null) 
                 continue;
             Joint j = findJoint(part,sp);
             if (j == null) {
                 //missing joint
             }
         }
     }    
 
     public static Joint findJoint(Part part1, Part part2) {
         foreach (Joint j in part1.GetComponents<Joint>()) {
             Part temp = j.connectedBody.GetComponent<Part>();
             if (temp == part2) {
                 return j;
             }
         }
         foreach (Joint j in part2.GetComponents<Joint>()) {
             Part temp = j.connectedBody.GetComponent<Part>();
             if (temp == part1) {
                 return j;
             }
         }
         
         return null;
     }
 
               There you go.
Answer by mrlinds · Mar 23, 2018 at 08:18 PM
I recently ran into this issue, but found a better solution, likely because of Unity updates. In Unity 2018 you can now check joint.currentForce. Not sure when that was added
 private void OnJointBreak(float breakForce) {
         for ( int i = 0; i < joints.Count; i++ ) {//have list of joints
             ConfigurableJoint joint = joints[i];
             if (breakForce != joint.currentForce.magnitude ) {
                 continue;
             }
             RemoveJoint(joint); //do joint cleanup
             break;
         }
     }
 
              It is not a good idea to check inequality between two floats using the != operator. Floats may have precision errors. You should use $$anonymous$$athf.Approximately
Your answer
 
             Follow this Question
Related Questions
How do you find a Joint from its connectedBody? 1 Answer
Connecting gameobject to specific part of player 1 Answer
How to add friction to Unity HingeJoint? 1 Answer
How target rotation and target angular velocity work in configurable joint ? 1 Answer
Unity DOTS Physics constraint linear and angular x,y,z 0 Answers