Legacy Documentation: Version 4.6(go to latest)
Language: English
  • C#
  • JS
  • Boo

Script language

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

Rigidbody.AddExplosionForce

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

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