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


Gõ tìm kiếm nhanh...