- Home /
Meteor Explosion, Destroy on Impact
Hi Guys,
First off, I know this is a simple question, and I have tried many tactics to get this to work, but I can't seem to get this working properly. I have been using the "Detonator-Insanity" Prefab to be the effect, but I get nothing.
So, what I want to happen is a meteor to fall out of the sky, explode on impact with the terrain, destroy itself, and once it explodes, I want to start a new reaction where it releases toxins and other interesting things.
I really only need help with the meteor exploding on impact and destroying itself. I use JS. I'm hoping for some code examples, as I have tried all the examples that I could find, and yet I still had no success.
Thank you for your help guys.
Thank you for your suggestion. I definitely have some basic knowledge of colliders, but I do not know anything about explosion commands. I just have not been able to make things work. I will show some scripts that I have used.
I tried approaching this from a health angle. I thought the meteor should have health, and when it hit the terrain, it should lose health. This is what I tried, and attached to the meteor, but it just hit the ground. No effect.
var hp : int = 50;
function Update (){
if (hp==0){
gameObject.GetComponent("Detonator").Explode();
}
}
function OnCollisionEnter(collision:Collision){
if(collision.rigidbody){
hp = hp - 50;
}
}
I also found this code, but it did not work.
// A grenade
// - instantiates a explosion prefab when hitting a surface
// - then destroys itself
var explosionPrefab : GameObject;
var explodeSecs : float = -1;
function Awake()
{
if (explodeSecs > 0) {
Invoke ("DestroyNow", explodeSecs);
}
}
function OnCollisionEnter( collision : Collision )
{
// Rotate the object so that the y-axis faces along the normal of the surface
var contact : ContactPoint = collision.contacts[0];
var rot : Quaternion = Quaternion.FromToRotation (Vector3.up, contact.normal);
var pos : Vector3 = contact.point;
Instantiate(explosionPrefab, pos, rot);
// Destroy the projectile
Destroy (gameObject);
}
function DestroyNow()
{
Instantiate(explosionPrefab, transform.position, transform.rotation);
Destroy (gameObject);
}
Answer by Jessespike · Oct 28, 2012 at 06:32 PM
I wouldn't recommended this method, but it'll probably work. Colliders would be a better choice.
#pragma strict
public var minimumYPosition : float = 0f;
public var nextPrefabToLoad : GameObject;
function Update () {
if (this.transform.position.y < minimumYPosition)
{
if (nextPrefabToLoad != null) {
GameObject.Instantiate(nextPrefabToLoad, transform.position, transform.rotation);
}
Destroy(this.gameObject);
}
}
Edit
I don't know what a detonator is, a prefab that plays an animation clip? What does Expode() do? Where is the detonator, on the meteor or the explosion insanity? There's just not enough to go by on.
function Update () {
if (this.transform.position.y < minimumYPosition)
{
if (nextPrefabToLoad != null) {
var explosion : GameObject = GameObject.Instantiate(nextPrefabToLoad, transform.position, transform.rotation);
if (explosion.GetComponent("Detonator") != null) {
explosion.GetComponent("Detonator").Explode(); //detonator on explosion insanity
}
}
//gameObject.GetComponent("Detonator").Explode(); // detonator on meteor
if (this.renderer.enabled == true) Destroy(this.gameObject, 60f);
this.renderer.enabled = false;
}
}
O$$anonymous$$, partial credit given. I know the code functions as it should in that this code causes the meteor to be destroyed, but no detonation is triggered, which is what I need to happen. Any assistance? I add the detonation-Insanity, but it does not get triggered on impact with the terrain. This has been my main problem the entire time.
So, the detonator, is a prefab. It contains a bunch of scripts with materials and particles attached to it. You can edit a bunch of the variables in these scripts. I just can't get the meteor to cause the detonator to trigger. Did you see my onCollision script? I just can't the meteor to explode. The detonator allows me to create the kind of explosion I want. But nothing is happening as of right now.
Ya i looked at the OnCollision code, I noticed, this:
gameObject.GetComponent("Detonator").Explode();
which may be causing a issue. If you look at my edit, you'll see I get the component from the instantiated object and not on the local gameObject.
O$$anonymous$$, I don't know why I said that I couldn't make it work before, but the OnCollide script works, it just doesn't throw out the explosion. It just destroys on impact. All the other scripts are the ones that do not work.
Answer by Dr_GeoFry · Oct 28, 2012 at 11:53 PM
Not sure if I ever had a problem now. The explosion worked, it was just so small that I couldn't see it from the distance I was looking toward it. I created a camera and followed the meteor, and I saw it explode. Thank you all for taking the time to assist me.
Your answer
Follow this Question
Related Questions
Tank Missle Colide then explode and destroy 1 Answer
Explosion Elimination script 2 Answers
how can I destroying a spawn object ? 1 Answer
Detect explosion Character Controller 4 Answers
scripting Impact explosion 1 Answer