Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

SOLVED: OnJointBreak

Discussion in 'Editor & General Support' started by eekllc, Jan 30, 2010.

  1. eekllc

    eekllc

    Joined:
    Dec 14, 2009
    Posts:
    19
    I have several joints attached to a rigidbody, but i"m interested in knowing when a few particular joints break. The OnJointBreak function seems to pass only the force information to the event, which is pretty useless to me in discerning which joint broke.

    Is there a better way to detect this?
     
  2. eekllc

    eekllc

    Joined:
    Dec 14, 2009
    Posts:
    19
    In case anybody's interested, I found a solution for this. I noticed it posted a few other places in this forum.

    The best way I can see of how to do it is to create your joint in code and keep a reference to it.

    Here's a small function I made that creates one:

    Code (csharp):
    1.  
    2. public static FixedJoint CreateFixedJoint(Transform parent, Transform attachedTo, float breakForce)
    3.     {
    4.         FixedJoint result = parent.gameObject.AddComponent<FixedJoint>();
    5.         result.connectedBody = attachedTo.rigidbody;
    6.         result.breakForce = breakForce;
    7.         return result;
    8.     }
    9.  
    And you would assign it by the following code:

    Code (csharp):
    1.  
    2. FixedJoint fixedJoint = CreateFixedJoint(coreRigidBody, appendageRigidBody, 3000);
    3.  
    When the joint breaks, it will become null. So wherever you're using it, simply check for it by going:

    Code (csharp):
    1. if (fixedJoint != null)
    If it comes back true, the joint is still in tact. If it comes back false, the joint has been broken.

    This takes advantage of the fact that Unity destroys the joints after they are no longer needed.
     
  3. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    I tried the same approach and found out that the joint (I use the configurable one) doesn't get destroyed instantly, I have to wait till the next frame to check the joint state.

    Code (csharp):
    1. private IEnumerator OnJointBreak(float breakForce)
    2. {
    3.     Debug.Log("OnJointBreak");
    4.     yield return null;
    5.     if (joint == null)
    6.     {
    7.         // ...
    8.     }
    9. }
     
    RandomPoint and Oxygeniium like this.
  4. MESSI007

    MESSI007

    Joined:
    Feb 18, 2017
    Posts:
    4
  5. RandomPoint

    RandomPoint

    Joined:
    Jul 21, 2017
    Posts:
    14
    OnJointBreak seems like a badly-designed message callback. The Joint that broke should be passed in as an argument.
     
  6. xavier513

    xavier513

    Joined:
    May 16, 2019
    Posts:
    8
    Having the same issue. Reference to the broken Joint should be passed in as an argument for OnJointBreak().