- Home /
Answer by Joshua · Feb 27, 2011 at 12:03 PM
I don't know why people keep posting answers here where they use triggers wrong. Triggers are intended to for instance make an invisible forcefield that when you walk through it the door in front of you slides open. You 'can' use them for collision, but it's easier to just use:
var score : int = 10;
function OnCollisionEnter (col : Collision) {
if(col.gameObject.Tag == "Player"){
score -=1;
Destroy(gameObject);
}
}
Now all you have to do is attach this to the rotating object and make sure your playercharacter has the tag Player. :)
Just wanted to point out the accepted answer should be: OnCollisionEnter and not OnCollisSionEnter . At least I guess so...
I believe that it is because of the unnecessary calculations involved if you are not actually using anything from the Collision class. If you just want to know if one gameObject entered another gameObject, then using a trigger is fine. From Unity: In contrast to OnTriggerEnter, OnCollisionEnter is passed the Collision class and not a Collider. The Collision class contains information about contact points, impact velocity etc. If you don't use collisionInfo in the function, leave out the collisionInfo parameter as this avoids unneccessary calculations. Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached. http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html
Answer by AVividLight · Feb 27, 2011 at 05:15 AM
This is what I would use...
var score : int = 10; var objectTag : String = "Put The GameObjects Tag Here";
function OnCollissionEnter (collidedObject : Collision) { if(collidedObject.gameObject.tag == objectTag){ score -=1; Destroy(gameObject); } }
-Hope I Helped!
Thanks Joshua for the original code...
Yeah, that's even neater. I'm fairly new to scripting myself and trying to make my style more neat, so thanks. :)
Yeah, I'm glad I could help, and I wish you luck in your hunt for gold (Learning Scripting) :)
Answer by Noah-1 · Feb 27, 2011 at 04:56 AM
It would be something like this:
var MyVar = 10;
function OnTriggerEnter (other : Collider) {
if (other.CompareTag ("HERE YOUR TAG NAME")) {
Destroy (gameObject);
MyVar--;
} }
I hope it helps, good Luck.