public List <GameObject> FXPrefabs = new List <GameObject>(); // list of special effects objects to spawn
///////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////
void Awake()
{
// spawn effects gameobjects
foreach (GameObject fx in FXPrefabs)
{
Component[] c;
c = fx.GetComponents <vp_Explosion>();
if (c.Length == 0)
{
Object.Instantiate(fx, transform.position, transform.rotation);
}
else
{
Debug.LogError("Error: vp_Explosion->FXPrefab must not be a vp_Explosion (risk of infinite loop).");
}
}
// apply shockwave to all rigidbodies and FPSPlayers within range
Collider[] colliders = Physics.OverlapSphere(transform.position, Radius);
foreach (Collider hit in colliders)
{
if (hit != this.collider)
{
//Debug.Log(hit.transform.name + Time.time); // snippet to dump affected objects
if (hit.rigidbody)
{
float distanceModifier = (1 - Vector3.Distance(transform.position,
hit.transform.position) / Radius);
// explosion up force should only work on grounded objects,
// otherwise object may acquire extreme speeds
Ray ray = new Ray(hit.transform.position, -Vector3.up);
RaycastHit hit2;
if (!Physics.Raycast(ray, out hit2, 1))
{
UpForce = 0.0f;
}
// bash all objects within range
hit.rigidbody.AddExplosionForce(Force, transform.position, Radius, UpForce);
if (hit.gameObject.layer != vp_Layer.Debris)
{
hit.gameObject.SendMessage(DamageMethodName, distanceModifier * Damage,
SendMessageOptions.DontRequireReceiver);
}
}
else
{
vp_FPSController controller = hit.GetComponent <vp_FPSController>();
if (controller)
{
float distanceModifier = (1 - Vector3.Distance(transform.position,
hit.transform.position) / Radius) * 0.5f;
controller.AddForce((controller.transform.position - transform.position).normalized *
Force * 0.001f * distanceModifier);
vp_FPSCamera camera = hit.GetComponentInChildren <vp_FPSCamera>();
if (camera)
{
camera.DoBomb(distanceModifier * CameraShake);
}
if (hit.gameObject.layer != vp_Layer.Debris)
{
hit.gameObject.SendMessage(DamageMethodName, distanceModifier * Damage,
SendMessageOptions.DontRequireReceiver);
}
}
}
}
}
// play explosion sound
audio.clip = Sound;
audio.rolloffMode = AudioRolloffMode.Linear;
audio.minDistance = 3;
audio.maxDistance = 250;
audio.dopplerLevel = 2.5f;
audio.pitch = Random.Range(SoundMinPitch, SoundMaxPitch);
audio.playOnAwake = false; // 'playOnAwake' seems unreliable, so play manually
audio.Play();
}