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
public class BreakableSurface : MonoBehaviour
{
public float _BreakWeight;
public void OnTriggerEnter(Collider other)
{
Debug.Log("BreakableSurface OnTriggerEnter " + other.name);
Transform parent = transform.parent;
Rigidbody[] _RigidbodyList = parent.GetComponentsInChildren<Rigidbody>();
foreach (Rigidbody rb in _RigidbodyList)
{
rb.drag = Random.Range(.5f, 3f);
rb.AddTorque(Random.Range(100, 300), Random.Range(100, 300), Random.Range(100, 300));
rb.isKinematic = false;
}
}
}
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
You cannot add forces or torque to a Kinematic Rigidbody. You should turn off Kinematic body type before you try to do so.
1 Like
awesome, so all i had to do was put rb.isKinematic = false; before addTorque
this code works great now
foreach (Rigidbody rb in _RigidbodyList)
{
rb.drag = Random.Range(.5f, 3f);
rb.isKinematic = false;
rb.AddTorque(Random.Range(0, 30), Random.Range(0, 30), Random.Range(0, 30));
}
1 Like
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
By ensuring you have colliders on the “wood planks” and that they are set to actually collide with the terrain and its collider(s).
1 Like
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?
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.
1 Like
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
public class BreakableSurface : MonoBehaviour
{
public float _BreakWeight = 30; // break at 30kg, a bit less than player
public void OnTriggerEnter(Collider other)
{
Debug.Log("BreakableSurface OnTriggerEnter " + other.name);
Rigidbody otherRb = other.transform.GetComponent<Rigidbody>();
if (!otherRb)
{
Debug.LogError("BreakableSurface OnTriggerEnter " + other.name + "has no Rigidbody");
return;
}
if (otherRb.mass < _BreakWeight)
return;
Rigidbody[] _RigidbodyList = transform.parent.GetComponentsInChildren<Rigidbody>();
foreach (Rigidbody rb in _RigidbodyList)
{
rb.drag = Random.Range(.5f, 3f);
rb.isKinematic = false;
rb.AddTorque(Random.Range(0, 30), Random.Range(0, 30), Random.Range(0, 30));
}
Collider[] _ColliderList = transform.parent.GetComponentsInChildren<Collider>();
foreach (Collider col in _ColliderList)
col.enabled = true;
}
}
1 Like