Legacy Documentation: Version 4.5.0

Script language:

  • JS
  • C#
  • Boo
Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Rigidbody.AddExplosionForce

Switch to Manual
AddExplosionForce(explosionForce: float, explosionPosition: Vector3, explosionRadius: float, upwardsModifier: float = 0.0F, mode: ForceMode = ForceMode.Force): void;
void AddExplosionForce(float explosionForce, Vector3 explosionPosition, float explosionRadius, float upwardsModifier = 0.0F, ForceMode mode = ForceMode.Force);
def AddExplosionForce(explosionForce as float, explosionPosition as Vector3, explosionRadius as float, upwardsModifier as float = 0.0F, mode as ForceMode = ForceMode.Force) as 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. explosionRadius 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. explosionRadius 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.

	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 && hit.rigidbody)
				hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0);
		}
	}
using UnityEngine;
using System.Collections;

public class ExampleClass : 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 && hit.rigidbody)
                hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0F);
            
        }
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	public radius as float = 5.0F

	public power as float = 10.0F

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