How would I go about writing a script which when a grenade prefab which I have, collides with anything in my world it would destroy itself and give of an explosion which I have made with a particle effect?
4 Answers
4It’s simple: just use OnCollisionEnter to fire the explosion:
var explosion: GameObject; // drag your explosion prefab here
function OnCollisionEnter(){
var expl = Instantiate(explosion, transform.position, Quaternion.identity);
Destroy(gameObject); // destroy the grenade
Destroy(expl, 3); // delete the explosion after 3 seconds
}
In C#:
public GameObject explosion; // drag your explosion prefab here
void OnCollisionEnter(){
GameObject expl = Instantiate(explosion, transform.position, Quaternion.identity) as GameObject;
Destroy(gameObject); // destroy the grenade
Destroy(expl, 3); // delete the explosion after 3 seconds
}
You can add the explosion sound to the explosion prefab - with Play On Awake set (is the default option) it will play automatically when the explosion is created.
and if the explosion is a particle system, you can check autodestruct (and you may want to check one shot), so there’s no need to destroy it manually, and you can be sure that it it run a single animation, and then it will destroy it self
I don't get it. Using a particle effect prefab from the sample assets folder and it just explodes as soon as I hit start even after using the script above on a trigger.Looking at the two scripts attached to the prefab I don't see anything on how to control it there either
– shadowpuppetThat would happen if you put the particle effect prefab in the hierarchy. It should exist in the project folder only. Then it will explode only when it is instantiated by the OnCollisionEnter function. @shadowpuppet
@ysimkin. Been a while since I have been in Unity, but I had a similar situation where if my motorcycle hit a wall above a certain speed it would explode, below that it would just collide. I would guess on your instantiated prefab write in some code for velocity. Looking at my scripts…
using UnityEngine;
using System.Collections;
float crashSpeed;
float CurrentSpeed;
void Update():
CurrentSpeed = transform.InverseTransformDirection(rigidbody.velocity).z;
}
if(CurrentSpeed> crashSpeed);
//explode script here, instantiate something
}
}
As in, is that what you want, or is it doing that already? If there's a delay before the explosion, check your prefab particle effect to make sure that it looks right.
– syclamothOh yeah, if you use AutoDestruct on the explosion, it will delete itself- no need to use the 'Destroy(timer)' after instantiation.
– syclamothhey , i'm trying to convert this java script to c# , but having trouble with this line : var expl = Instantiate(explosion, transform.position, Quaternion.identity); instead of "var" i need to know the datatype used in c# ...help plz !
– rangapraveenIt's the same type as the prefab (GameObject, in this case), and you must convert Instantiate to the same type: GameObject expl = Instantiate(explosion, transform.position, Quaternion.identity) as GameObject;
– aldonalettoperfect ... thanx :)
– rangapraveen