- Home /
How do I make an object disappear when a bullet hits it?
I'm making a zombie game and I want to make it so that if a bullet Object collides with the zombie Object, the zombie object disappears. This script will be attached to the bullet, but if it has to be attached to the zombie, please say so.
thanks!
Answer by xt-xylophone · Mar 21, 2013 at 04:04 AM
As long as both have a collider you you should be able to do the script on either.
Use the onCollisionEnter function, check the docs for usages. Create some tags, google how to do that too. Maybe make 2, 1 called Zombie and the other called Bullet. Add those tags to their respective objects.
Then in your collision function you can check if the thing it collided with is a certain tag. Like: collision.gameObject.tag == "Zombie". Use the tags because the collision function will happen when the zombie touches the floor or the player too.
If that's true then you know one has hit the other then simply use a destroy function on them both. I think the order matters like if the script is in the zombie, destroy the bullet before the zombie because then the script will be destroyed before it destroys the bullet.
Have fun!
Your answer
Follow this Question
Related Questions
how to have bullets produce particles? 2 Answers
RayCast Shooting And Errors 1 Answer
my sccripts/Health.js(26,26): BCE0019: 'Damage' is not a member of 'Bullet'. 0 Answers
Ricochet Trajectories C# 1 Answer
cant destroy other object 1 Answer