- Home /
How to check for a collisoin between objects
How would I check for a collision between 2 objects some thing like. I looked around but couldn't find a good answer.
if( sword collides with badguy){ destroy(badguy); }
It would be best for any examples to be c# but I dont mind either way
Thanks
Comment
Best Answer
Answer by xortrox · Jun 25, 2011 at 04:28 AM
If you had searched the words "unity" and "collision" on google this would of been the first choice:
http://unity3d.com/support/documentation/ScriptReference/Collision.html
anyhow.
you need to have a collider on the sword and the badguy, make sure the sword collider has "isTrigger" checked in the editor. Then in the sword script you do:
function OnTriggerEnter(iCollider:Collider) {
if(iCollider.transform.name == "name-of-badguy-object")
{
Destroy(iCollider.gameObject);
/*
use this if you want to destroy the badguy after some time (in seconds):
var TIME:int = 5;
Destroy(iCollider.gameObject,TIME);
*/
}
}
This should work :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612121327im_/https://answers.unity.com/themes/thub/images/avi.jpg)