Created
September 27, 2013 18:07
-
-
Save keiranlovett/6732641 to your computer and use it in GitHub Desktop.
Simple Explosion Effect. Instantiate along with particles to throw nearby rigidbodies away from the explosion. Author: Opless
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class OnExplosionEffect : MonoBehaviour { | |
public float radius = 5; | |
public float power = 5; | |
public float upwardForce = 0; | |
private float radiusUsed = 0.5F; | |
// Update is called once per frame | |
void FixedUpdate () | |
{ | |
// Applies an explosion force to all nearby rigidbodies | |
Vector3 explosionPos = transform.position; | |
Collider[] colliders = Physics.OverlapSphere (explosionPos, radius); | |
foreach (Collider hit in colliders) | |
{ | |
if(hit == null) | |
continue; | |
if (hit.rigidbody) | |
{ | |
hit.rigidbody.AddExplosionForce(power, explosionPos, radiusUsed, upwardForce); | |
} | |
} | |
radiusUsed = ((radius-radiusUsed)/2)*Time.deltaTime; | |
} | |
// Auto destroy | |
public float timeOut = 1.0F; | |
void Awake () | |
{ | |
Invoke ("DestroyNow", timeOut); | |
} | |
void DestroyNow () | |
{ | |
DestroyObject (gameObject); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment