Destroy a GameObject when it Collides with any other GameObject
I have a projectile that I want to be destroyed as soon as it collides with any other GameObject in the world. I have only been able to find solutions for destroying objects with OnCollisionEnter that involve checking for a specific tag or name. This is not a good solution for me as I want the projectile to be destroyed upon colliding with anything.
Here is what I've been trying. Using this code:
void OnCollisionEnter (Collision collision)
{
Destroy(gameObject);
}
...the projectile is destroyed as soon as it spawns. Appears in the spot it is supposed to be fired from then instantly disappears. Next I tried this:
void OnCollisionEnter (Collision collision)
{
if(gameObject.tag != "MagShot")
{
Destroy(gameObject);
}
}
"MagShot" is the tag on the projectile, so basically I'm checking if it collides with anything other than itself. This way, the projectile spawns and fires fine, but is not destroyed when it collides with another GameObject.
How do I simply destroy a GameObject as soon as it collides with anything else in the scene?
Answer by Eno-Khaon · Oct 22, 2015 at 10:12 PM
Well, if your projectile is disappearing immediately upon creation, then that's because it's doing exactly what you're expecting; it's destroyed upon contact with the first thing it reaches.
Now, it's not what you're hoping for, but that's simply the current state of things. Here's what is most likely occurring, more precisely:
The projectile is created at the tip of a gun, let's say. The bullet is embedded slightly inside the gun so that it appears to fly out of the barrel upon creation... and therein lies the problem.
The projectile is (in your case) most likely colliding with whatever is firing it when it's created. As a result, your bullet recognizes that it hit something, regardless of what it is, and then destroyed itself.
So, how do you account for this? One relatively simple way is to put both the weapon and its projectiles on the same layer as each other and disable collisions between objects sharing that layer. Alternatively, if you'd prefer that bullets be able to collide with each other, put the bullets on a second layer and only disable collision between bullets and gun layers, but not bullet and bullet.
For an alternative approach, in case you want a gun to be able to shoot another player's gun (as applicable), Physics.IgnoreCollision() states clearly that "This is useful, say, for preventing projectiles from colliding with the object that fires them."
So this is what I assumed to be the problem initially (the projectile colliding with the character that spawns it) but since the point the projectile was spawning from is an empty object that is not actually touching the character, I thought that must not be the issue. But I tried again and I suppose the range of the character's collider must extend beyond the actual collider itself, because I simply moved it further away and now it works.
Yup, all this talk of layers. Sometimes the answer is a lot simpler.
Answer by CaKeMeaT · Oct 22, 2015 at 10:14 PM
because of the brilliance of physics layers, and your ability to choose which ones collide with the others, I would REALLY recommend you actually use tags. If you cant come up with a naming convention ("walls", "enemies", "obstacles", etc) then you might want to reconsider the overall design of your game.
Answer by meat5000 · Oct 22, 2015 at 11:01 PM
the projectile is destroyed as soon as it spawns
Its colliding with your gun! Move the spawn point forward past the Player's colliders.
Follow this Question
Related Questions
Unity3d OnCollisionEnter not firing 1 Answer
Advance Colision Detection 1 Answer
How can i restart a script on collision ? 1 Answer