Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

how to make object tumble randomly toward the ground?

Discussion in 'Physics' started by steveh2112, Jan 22, 2020.

  1. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    i have a breakable bridge, when i walk on it i get a trigger, the breakable treads have rigid bodies (no constraints), this is the code
    Code (CSharp):
    1. public class BreakableSurface : MonoBehaviour
    2. {
    3.     public float _BreakWeight;
    4.  
    5.     public void OnTriggerEnter(Collider other)
    6.     {
    7.         Debug.Log("BreakableSurface OnTriggerEnter " + other.name);
    8.  
    9.         Transform parent = transform.parent;
    10.         Rigidbody[] _RigidbodyList = parent.GetComponentsInChildren<Rigidbody>();
    11.         foreach (Rigidbody rb in _RigidbodyList)
    12.         {
    13.             rb.drag = Random.Range(.5f, 3f);
    14.             rb.AddTorque(Random.Range(100, 300), Random.Range(100, 300), Random.Range(100, 300));
    15.             rb.isKinematic = false;
    16.         }
    17.     }
    18. }
    19.  
    they fall ok at random speeds but the AddTorque line seems to do nothing, they all fall flat, i want them to tumble. i tried several different values.
    any ideas? thanks
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,328
    You cannot add forces or torque to a Kinematic Rigidbody. You should turn off Kinematic body type before you try to do so.
     
    steveh2112 likes this.
  3. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    awesome, so all i had to do was put rb.isKinematic = false; before addTorque
    this code works great now
    Code (CSharp):
    1. foreach (Rigidbody rb in _RigidbodyList)
    2.         {
    3.             rb.drag = Random.Range(.5f, 3f);
    4.             rb.isKinematic = false;
    5.             rb.AddTorque(Random.Range(0, 30), Random.Range(0, 30), Random.Range(0, 30));        
    6.         }
     
    MelvMay likes this.
  4. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    one more question, my wood planks fall through the terrain and they are gone. how would make it so then collide with the terrain and stay put? thanks
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,328
    By ensuring you have colliders on the "wood planks" and that they are set to actually collide with the terrain and its collider(s).
     
    steveh2112 likes this.
  6. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    oh right, i forgot, i didn't bother to put colliders on the individual planks because i have a trigger zone that covers half a dozen planks in the breakable area. guess i need to add.

    for performance reasons, i guess its best to enable those colliders only after the trigger? there's only a handful of them so maybe not worth the trouble. what do you think?
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,328
    It's not clear what your set-up is and TBH only you, via the profiler, can determine what the performance is like but it makes sense to not have colliders active that don't need to be. I guess the main reason to not have the colliders active would be that you'd get collisions on them that you wouldn't want until triggered.
     
    steveh2112 likes this.
  8. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    thanks again, i decided only to add colliders to a few planks so the area where the player falls doesn't get too cluttered. seems i need to disable the tread colliders when they are on the bridge or they fall down.

    so here is the final working code
    Code (CSharp):
    1. public class BreakableSurface : MonoBehaviour
    2. {
    3.     public float _BreakWeight = 30;     // break at 30kg, a bit less than player
    4.  
    5.     public void OnTriggerEnter(Collider other)
    6.     {
    7.         Debug.Log("BreakableSurface OnTriggerEnter " + other.name);
    8.  
    9.         Rigidbody otherRb = other.transform.GetComponent<Rigidbody>();
    10.         if (!otherRb)
    11.         {
    12.             Debug.LogError("BreakableSurface OnTriggerEnter " + other.name + "has no Rigidbody");
    13.             return;
    14.         }
    15.  
    16.         if (otherRb.mass < _BreakWeight)
    17.             return;
    18.  
    19.         Rigidbody[] _RigidbodyList = transform.parent.GetComponentsInChildren<Rigidbody>();
    20.         foreach (Rigidbody rb in _RigidbodyList)
    21.         {
    22.             rb.drag = Random.Range(.5f, 3f);
    23.             rb.isKinematic = false;
    24.             rb.AddTorque(Random.Range(0, 30), Random.Range(0, 30), Random.Range(0, 30));        
    25.         }
    26.  
    27.         Collider[] _ColliderList = transform.parent.GetComponentsInChildren<Collider>();
    28.         foreach (Collider col in _ColliderList)
    29.             col.enabled = true;
    30.     }
    31. }
     
    Last edited: Jan 22, 2020
    MelvMay likes this.