Search Unity

Bug OnJointBreak() not calling when i set useSpring=true in script.

Discussion in 'Scripting' started by MRBAEK, Aug 11, 2020.

  1. MRBAEK

    MRBAEK

    Joined:
    Sep 30, 2014
    Posts:
    13
    OnJointBreak() not calling when i set useSpring=true in script.

    i'm currently implementing the ability to destruct the bonnet(hood) of the vehicle.

    initial breakForce are Infinity.

    i don't want the joint on the car bonnet to be destroyed immediately.

    the initial value of breakForce in the car is Infinity, and the Use Spring is unchecked.

    i want to set the useSpring as true for the first impact to show the shape of the bonnet rattling, and to break the joint when the second impact occurs.

    here is my code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Test_DestructableJoint : MonoBehaviour
    5. {
    6.     private CarController car;
    7.     private HingeJoint joint;
    8.  
    9.     private void Start()
    10.     {
    11.         car = GetComponentInParent<CarController>();
    12.         joint = GetComponent<HingeJoint>();
    13.     }
    14.  
    15.     private void OnCollisionEnter(Collision collision)
    16.     {
    17.         if(car.Speed > 100.0f && !joint.useSpring)
    18.         {
    19.             Debug.Log("OnCollisionEnter()");
    20.             joint.breakForce = 2000.0f;
    21.             joint.useSpring = true;
    22.         }
    23.     }
    24.  
    25.     private void OnJointBreak(float breakForce)
    26.     {
    27.         Debug.Log("OnJointBreak()");
    28.         transform.SetParent(null);
    29.         gameObject.layer = LayerMask.NameToLayer("Default");
    30.     }
    31. }
    32.  
    when use this code, spring is set to true and it works to my surpose.

    but when i give impact second impact, joint are not break, OnJointBreak() are not called.

    at this state, i set 'use spring' check box's turn off and turn on again in inspector tab, it works.

    why joint's act like this?

    am i use wrong approach?

    best regards.