Reputation: 23
What I want is to destroy the cube when the third person camera touches it...but anything i have tried so far fails...
Here is the code i have tried:
#pragma strict
var other : GameObject;
function Start () {
}
function Update () {
}
function OnCollisionEnter ( collision : Collision) {
if (collision.tag == "Character")
Destroy (collision.gameObject);
}
Thanks for any suggestions!
Upvotes: 0
Views: 1301
Reputation: 97
There are two simple ways to do this. One of them is attaching a script to the character to destroy specified objects and the other one is to attach a script to the object to be destroyed on inpact with the character, but on both of those ways you NEED the Rigidbody component to be attached too.
Adding this to the object to be destroyed and tagging the character:
[RequireComponent (typeof (Rigidbody))]
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "Character")
Destroy(this.gameObject);
}
OR
Adding this to the character and tagging the objects to be destroyed:
[RequireComponent (typeof (Rigidbody))]
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "ToBeDestroyed")
Destroy(col.gameObject);
}
remember: this code is in C#, you`ll need to convert to javascript if you are going to add to an existing script
Upvotes: 1