Rigidbody.AddExplosionForce Manual     Reference     Scripting  
Scripting > Runtime Classes > Rigidbody
Rigidbody.AddExplosionForce

function AddExplosionForce (explosionForce : float, explosionPosition : Vector3, explosionRadius : float, upwardsModifier : float = 0.0F, mode : ForceMode = ForceMode.Force) : void

Description

Applies a force to the rigidbody that simulates explosion effects. The explosion force will fall off linearly with distance to the rigidbody.

The function also plays nicely with ragdolls. If radius is 0, the full force will be applied no matter how far away position is from the rigidbody. upwardsModifier applies the force as if it was applied from beneath the object. This is useful since explosions that throw things up instead of pushing things to the side look cooler. A value of 2 will apply a force as if it is applied from 2 meters below while not changing the actual explosion position. explosionPosition is the position from which the explosion force is to be applied. explosionRadius is the radius of the explosion. Rigidbodies further away than explosionRadius will not be affected.

JavaScripts
var radius = 5.0;
var power = 10.0;
function Start () {
// Applies an explosion force to all nearby rigidbodies
var explosionPos : Vector3 = transform.position;
var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);

for (var hit : Collider in colliders) {
if (!hit)
continue;

if (hit.rigidbody)
hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0);
}
}

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
public float radius = 5.0F;
public float power = 10.0F;
void Start() {
Vector3 explosionPos = transform.position;
Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
foreach (Collider hit in colliders) {
if (!hit)

if (hit.rigidbody)
hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0F);

}
}
}

import UnityEngine
import System.Collections

class example(MonoBehaviour):

public radius as single = 5.0F

public power as single = 10.0F

def Start():
explosionPos as Vector3 = transform.position
colliders as (Collider) = Physics.OverlapSphere(explosionPos, radius)
for hit as Collider in colliders:
if not hit:
continue
if hit.rigidbody:
hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0F)