Seven is the number.
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.

69 lines
2.3 KiB

  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using Random = UnityEngine.Random;
  5. namespace UnityStandardAssets.Effects
  6. {
  7. public class ExplosionFireAndDebris : MonoBehaviour
  8. {
  9. public Transform[] debrisPrefabs;
  10. public Transform firePrefab;
  11. public int numDebrisPieces = 0;
  12. public int numFires = 0;
  13. private IEnumerator Start()
  14. {
  15. float multiplier = GetComponent<ParticleSystemMultiplier>().multiplier;
  16. for (int n = 0; n < numDebrisPieces*multiplier; ++n)
  17. {
  18. var prefab = debrisPrefabs[Random.Range(0, debrisPrefabs.Length)];
  19. Vector3 pos = transform.position + Random.insideUnitSphere*3*multiplier;
  20. Quaternion rot = Random.rotation;
  21. Instantiate(prefab, pos, rot);
  22. }
  23. // wait one frame so these new objects can be picked up in the overlapsphere function
  24. yield return null;
  25. float r = 10*multiplier;
  26. var cols = Physics.OverlapSphere(transform.position, r);
  27. foreach (var col in cols)
  28. {
  29. if (numFires > 0)
  30. {
  31. RaycastHit fireHit;
  32. Ray fireRay = new Ray(transform.position, col.transform.position - transform.position);
  33. if (col.Raycast(fireRay, out fireHit, r))
  34. {
  35. AddFire(col.transform, fireHit.point, fireHit.normal);
  36. numFires--;
  37. }
  38. }
  39. }
  40. float testR = 0;
  41. while (numFires > 0 && testR < r)
  42. {
  43. RaycastHit fireHit;
  44. Ray fireRay = new Ray(transform.position + Vector3.up, Random.onUnitSphere);
  45. if (Physics.Raycast(fireRay, out fireHit, testR))
  46. {
  47. AddFire(null, fireHit.point, fireHit.normal);
  48. numFires--;
  49. }
  50. testR += r*.1f;
  51. }
  52. }
  53. private void AddFire(Transform t, Vector3 pos, Vector3 normal)
  54. {
  55. pos += normal*0.5f;
  56. Transform fire = (Transform) Instantiate(firePrefab, pos, Quaternion.identity);
  57. fire.parent = t;
  58. }
  59. }
  60. }