- Home /
Simple Explosion Script With Key Press
Greetings All,
I need an example of a very simple explosion script.
I've created a prefab rock object that contains smaller rock objects which I am trying to force outward with a key press.
I found this little script that works when the game starts but only on box objects.
Any example is greatly appreciated, suggestions are welcome, and links without detailed explanations don't help a whole lot.
/*
DestructibleObject.js
source: http://answers.unity3d.com/questions/48718/how-can-use-addexplosionforce-to-explode-scattered.html
*/
var radius = 1.0; var power = 50.0; //function Start ()
function OnCollisionEnter(collision : Collision) {
Debug.Log("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(collision.gameObject.tag=="Explosion") continue;
if (!hit) continue;
/*
not sure why i have to explicity name everything
in the scene with a rigidbody
*/
if (hit.name=="breakable_rock_normal") continue;
if (hit.name=="ground") continue;
if (hit.name=="MouseCollider") continue;
if (hit.name=="Plane") continue;
if (hit.name=="Terrain") continue;
if (hit.name=="altar") continue;
if (hit.name=="crypt") continue;
if (hit.name=="graves01") continue;
if (hit.name=="Main Camera") continue;
if (hit.rigidbody)
Debug.Log(hit.name);
hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 10.0);
}
/* Destroy(gameObject); this does not work... */
}
Still working on it...
Here is what I've pieced together so far. I've managed to move the object in an upward motion, but cannot "explode" my objects in the prefab...
// Initialize Script Variables var radius = 1.5; var power = 100.0; var playerScore : int = 0; var playerTag : String = "The Player Nemo"; var keyPressCount:float=0.0; // keypress counter function Update () {
if (Input.Get$$anonymous$$eyDown ("space")) // if spacebar is pressed { //Debug.Log ("SpaceBar $$anonymous$$eypress ", gameObject);
var bigBoulder = GameObject.FindGameObjectsWithTag ("Big Boulder Test"); // find and assign variable to Big Boulder Test object tag var explosionPos : Vector3 = transform.position; var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
// Debug.Log ("Debug 1", gameObject);
for (var hit : Collider in colliders) { if (!hit) { continue; Debug.Log ("if not hit...", gameObject); }
/
source: http://answers.unity3d.com/questions/21338/a-better-way-then-ifhitgameobjecttag.html /
if(hit.gameObject.tag == bigBoulder) { playerScore++; // increase player score hit.gameObject.Send$$anonymous$$essage("ApplyPlayerScore",playerScore); // send player score to display function
// Debug.Log ("DEBUG: Player Score, ", gameObject);
keyPressCount++; // increase keypress Debug.Log ("DEBUG: " + keyPressCount, gameObject);
continue;
Debug.Log ("DEBUG: Did Rock Explode??? ", gameObject);
}
if (hit.rigidbody) { Debug.Log ("DEBUG: RigidBody ", gameObject); hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0); } }
}
} // end function Update function ApplyPlayerScore (playerScore : float) { print ("Player Score: " + playerScore); } // end function ApplyPlayerScore
Your answer
Follow this Question
Related Questions
Implementing collision and key pressing at once 1 Answer
Different Explosion for Different Collisions 3 Answers
How to create a collider-trigger that will start an animation when you press E 0 Answers
How to make an object explode? 2 Answers
Detonation of an object on collision of character.... 1 Answer