- Home /
Enemy death help
I am new to scripting so here is my question. I know there must be some way to delete an enemy after two rigidbodys with tags hit a object but I can not figure this out. here is my code. All I need is that part. I have tried many different things but none of them will work. can someone show me a good tutorial or give an idea. thank you. (codes a js) Thank you everyone.
function OnCollisionEnter(theCollision : Collision){
if(theCollision.gameObject.tag=="Bullet") {
Destroy(gameObject);
}
}
Answer by Berenger · Jul 01, 2012 at 12:14 AM
I don't have a tutorial, but I can explain what's happening (as I imagine it).
When the physic engine simulate the movements since the last time, it's going to looks if there was any collision event. That means the objects we're talking about need a collider and something that makes them move during the physic loop, usually a rigidbody. Note that changing the transform.position of a rigidody won't raise an event, it need to be an AddForce (It could with kinematics, but let's keep it simple).
When an event occures, Unity send message to both objects. If at least one of them is a trigger, those message are of the form OnTriggerXXX, if not OnCollisionXXX.
I suppose you want your characters to die when they are touched by a bullet. If this script is attached to the character's gameobject, which have a collider (isTrigger false), if the bullet have a collider(isTrigger false), a rigidbody not kinematic and if you used AddForce to launch it, if both objects are on a layer that can collide between each other (Physic Manager), then it should work.
Also, this is my 1000th answer, here is my proudest realisation, to celebrate \o/
Your answer
Follow this Question
Related Questions
Collision Problem with Rigidbody? 1 Answer
Enemy Collision Wont Work! PLEASE HELP 0 Answers
How to add rigidbody with script? 1 Answer
Destroy Enemy Rigidbody after Jumping on Head? 3 Answers
How to make an enemy chase player, using collisions 2 Answers