Unity3D – Explosion Force – JavaScript
Create a scene with:
– Plane (Collider)
– Cube1 (Collider + Rigid Body)
– Cube2 (Collider + Rigid Body)
– Cube3 (Collider + Rigid Body)
– Sphere (SphereExplosion.js)
SphereExplosion.js:
#pragma strict var radius = 50.0; // radius of explosion var power = 500.0; // power of explosion var bombObject : GameObject; // Assign in Inspector the Bomb GameObject function Start () { } function OnMouseDown () { // When you click over the object // Force START ----------------------------------------------------------- // Applies an explosion force to all nearby rigidbodies var explosionPos : Vector3 = bombObject.transform.position; // Returns an array with all colliders touching or inside the sphere. 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); } // Force END ------------------------------------------------------------- } function Update () { }
Inspector> Assign the Sphere Mesh to var bombObject, setup var radius and var power
Play and click over the Sphere… boom!!!