IEnumerator Start() { parentArray = new GameObject[128]; //Arbitrary array size; can be increased highestParent = 0; object[] sendArray = new object[2]; bool shook = false; Vector3 explosionPosition = transform.position; // Apply damage to close by objects first Collider[] colliders = Physics.OverlapSphere(explosionPosition, explosionRadius); foreach (var hit in colliders) { if (AlreadyHit(hit.gameObject) == false) { // Calculate distance from the explosion position to the closest point on the collider Vector3 closestPoint = hit.ClosestPointOnBounds(explosionPosition); float distance = Vector3.Distance(closestPoint, explosionPosition); // The hit points we apply fall decrease with distance from the explosion point float hitPoints = 1.0f - Mathf.Clamp01(distance / explosionRadius); if (hit.tag == "Player" && !shook) { shook = true; CameraShake.ShakeCam(Mathf.Max(hitPoints * shakeFactor, minShake), 10, Mathf.Max(hitPoints * shakeFactor, .3f)); } hitPoints *= explosionDamage; // Tell the rigidbody or any other script attached to the hit object how much damage is to be applied! if (hit.gameObject.layer != 2) { sendArray[0] = (object)hitPoints; sendArray[1] = (object)true; hit.SendMessageUpwards("ApplyDamage", sendArray, SendMessageOptions.DontRequireReceiver); hit.SendMessageUpwards("Direction", transform, SendMessageOptions.DontRequireReceiver); } } } if (!shook) { shook = true; CameraShake.ShakeCam(minShake, 10, minShake); } // Apply explosion forces to all rigidbodies // This needs to be in two steps for ragdolls to work correctly. // (Enemies are first turned into ragdolls with ApplyDamage then we apply forces to all the spawned body parts) colliders = Physics.OverlapSphere(explosionPosition, explosionRadius); foreach (var hit in colliders) { if (hit.GetComponent <Rigidbody>() && hit.gameObject.layer != PlayerWeapons.playerLayer) { hit.GetComponent <Rigidbody>().AddExplosionForce(explosionPower, explosionPosition, explosionRadius, vFactor); } } // stop emitting particles if (GetComponent <ParticleEmitter>()) { GetComponent <ParticleEmitter>().emit = true; yield return(new WaitForSeconds(0.5f)); GetComponent <ParticleEmitter>().emit = false; } // destroy the explosion after a while Destroy(gameObject, explosionTimeout); }
public void Update() { CameraShake.ShakeCam(amplitude, 10, time); }