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.
Ifradius
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)