- Home /
collision script to destroy
ok so i have defined a game object variable. and i know how to destroy an object but i need an example script that says something like if collide with this game object then be destroyed
Answer by skovacs1 · Nov 01, 2010 at 10:38 PM
either on the thing that destroys the other
function OnCollisionEnter(collision : Collision) {
Destroy(collision.gameObject);
}
or on the things that are destroyed
function OnCollisionEnter(collision : Collision) {
//if(collision.gameObject.tag == "Destroyer")
Destroy(gameObject);
}
Answer by lampshade · Nov 02, 2010 at 01:27 AM
You could attach a collision script to your game object. If your game object is traveling at some rate (its velocity is > 0) then you can do something like:
public class CollisionScript : MonoBehaviour {
private void OnCollisionEnter(Collision collision) {
transform.rigidbody.velocity = Vector3.zero; // stop the moving object
// instantiate an effect
StartCoroutine(CollisionEffect());
}
private IEnumerator CollisionEffect() {
// has the effect stopped?
Destroy(gameObject, 1.5f);
}
}
Your answer
Follow this Question
Related Questions
Zombie Health Script Not working?? 1 Answer
Destroy cube on collision 1 Answer
OnCollisionStay for seconds then destroy 0 Answers
Destroy a GameObject 2 Answers
Why won't the collision work? 3 Answers