- Home /
Destroy Projectile on collision
I am making a first person shooter where the player fires projectiles, How do i make the projectiles get destroyed as soon as they collide with somthing that is not an enemy or player. The way it is right now is that the bullets will collide with the ground and bounce off the ground/wall. How do i make the bullet get destroyed only when the bullet collides with the ground and walls?
Answer by oliver-jones · Jun 24, 2011 at 05:59 PM
So you want the projectile to destroy when it collides with anything BUT the enemy or player?
What you want to do is first check to see what the projectile collides with (make sure your projectile has a collider attached:
function OnCollisionEnter(collision : Collision){
//next - check if we have collided with anything but player/enemy
if(collision.gameObject.tag = "Player" || collision.gameObject.tag = "Enemy"){
//do nothing - or apply damage maybe?
}
else{
Destroy(gameObject);
}
}
This has not been tested as I am not infront of Unity, but it should work.
Answer by Anxo · Jun 24, 2011 at 05:45 PM
Look at the scripting reference on how to use OnCollisionEnter function. you can use that on your bullet and add a Destroy(gameObject) inside.
Answer by flaviusxvii · Jun 24, 2011 at 05:45 PM
This has been asked ad nauseam. Please Google for an answer.
Your answer
Follow this Question
Related Questions
How to destroy bullet on collision! 2 Answers
Collision misses - possibly not bullet through paper! (now with video) 1 Answer
Bullet Effect 2 Answers
Why won't my bullets detect a collision and be destroyed?... 4 Answers
RayCast Problem : 2d 1 Answer