- Home /
Have bullets be destroyed when they collide with ANY collider
Hi,
Was wondering if there is a way to have a bullet (with collider) be destroyed when it hits any object's collider (enemy/wall, etc), rather than specifying an individual collider.
What's the best method to use? OnCollisionEnter, OnTriggerEnter, Ray casts, and how?
Thanks
Answer by Scribe · May 20, 2012 at 05:41 PM
hopefully I'm not mistaken in saying that this will check for any collisions with the bullet
function OnCollisionEnter(){
Destroy (gameObject);
}
Hope that works! Scribe
NOTES:
1- place this code in the bullet's script;
2- the bullet must be a rigidbody.
Correct me if I am wrong, but the bullet does not need a rigidbody as long as the gameObject it touches is a rigidbody, right?
@OrangeLightning : I almost asked that also lol ... but figure since they want the bullet to destroy itself when it hit's "anything" , then it's either the bullet has a rigidbody, or would have to make sure "everything" else has a rigidbody on it.
Yes, in theory at least one of them must be a rigidbody - but moving a non-rigidbody object do not awake the hit rigidbodies (the object passes through them), thus it's better to make the bullet a rigidbody.
Actually it's important that only one of the elements has a rigidbody. I f the bullet has one and the wall (for example) has one too, the collision will not be detected. If both have a rigidbody, one has to be convex
Your answer
Follow this Question
Related Questions
2 objects collide, need to destroy one 2 Answers
Objects do not destroy on Collision 1 Answer
How to only delete one of two collided objects? 1 Answer
How to destroy bullet on collision! 2 Answers