- Home /
Destroy GameObject on touch of Collider C#
I'm making a top down game where i have a gun and i want to execute the Destroy(gameObject) command when the bullet comes in contact will a Collider, I've tried finding an answer on here of the recommended/ most efficient way to achieve this but i've only found people referring to two Colliders touching, where i don't really want to add a Collider to the Bullet object because it'd be a waste of resources when it could just detect when it hits something that tells it to be destroyed.
So i was wondering do I create the Kill script on the collider that the bullet will contact with, or create the script to be inside the bullet (this would lead to each bullet checking itself for whether or not it's hitting anything so that could potentially be a performance issue.). And how would i implement the logic where the Collider or the RigidBody/ GameObject Bullet can do something on detection of collision?
I'd really appreciate any and all suggestions/ implementations because i'd really rather not write an inefficient implementation for something that will be used very often.
Image of the Bullet Prefab Object In Inspector:
Answer by upasnavig90 · Mar 02, 2018 at 06:17 AM
Hey, If you don't want to detect the collision with colliders, you can also use raycasting for the purpose. just check the distance between the bullet and to the object ray is hitting, if its comes less than some offset(you can define), destroy the object.