You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.1 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace UnityStandardAssets.Effects
  6. {
  7. public class ExplosionPhysicsForce : MonoBehaviour
  8. {
  9. public float explosionForce = 4;
  10. private IEnumerator Start()
  11. {
  12. // wait one frame because some explosions instantiate debris which should then
  13. // be pushed by physics force
  14. yield return null;
  15. float multiplier = GetComponent<ParticleSystemMultiplier>().multiplier;
  16. float r = 10*multiplier;
  17. var cols = Physics.OverlapSphere(transform.position, r);
  18. var rigidbodies = new List<Rigidbody>();
  19. foreach (var col in cols)
  20. {
  21. if (col.attachedRigidbody != null && !rigidbodies.Contains(col.attachedRigidbody))
  22. {
  23. rigidbodies.Add(col.attachedRigidbody);
  24. }
  25. }
  26. foreach (var rb in rigidbodies)
  27. {
  28. rb.AddExplosionForce(explosionForce*multiplier, transform.position, r, 1*multiplier, ForceMode.Impulse);
  29. }
  30. }
  31. }
  32. }