Skip to content

Instantly share code, notes, and snippets.

@keiranlovett
Created September 27, 2013 18:07
Show Gist options
  • Save keiranlovett/6732641 to your computer and use it in GitHub Desktop.
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
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