- Home /
How To Destroy An Item When It Touches Something?
I Need It So That When Spear Touches Man, Man Is Destroyed.
That's not a lot to go on, but if you are using colliders, triggers or something to deter$$anonymous$$e contact, you can do many things like Destroy(man); need to see code to answer
Answer by Rick74 · Aug 25, 2013 at 01:51 AM
Attach a collision object to spear ( perhaps a box collider? )
Attach another to the Man.
check the (is trigger) box on both collision objects
Attach a rigid body to spear
probably wanna uncheck gravity on rigid body (I assuming you don't want the object you've given a rigid body too become effected by gravity, you can also check the box "is kinematic" as well.
give the spear object a tag ("spear")
Attach this code to the man;
function OnTriggerEnter ( other : Collider )
{
if ( other.collider.tag == "spear" )
{
Destroy ( gameObject );
}
}
Sorry I gave you the Javascript answer, I'm sure the C# version isn't far off.
edit: my bad I forgot the brackets on gameObject!
Answer by Evan Stallings · Aug 25, 2013 at 02:01 AM
C# version, in case you use C#:
void OnTriggerEnter (Collider collider) { if (collider.gameObject.CompareTag ("TAG")) { Destroy (gameObject); } }
Place this on your "man" or player. Replace "TAG" with the tag of the item you are running into.