It would help me immensely to have this function fully explained, because I’ve spent now over a week with UnityScript and although I’ve found some good tutorials, what I’m missings is a full step-by-step explanation of a piece of a script. The thing that is puzzling me with this is firstly the OnTriggerEnter. This is mentioned in the script reference section, as part of the engine, so why is being defined here? Or is it being defined?
Then there is the term “other : collider”. I have seen things written as “item : something” but not “other”.
It has the word “this”, which I am struggling to understand.
“Return”. Return to what or where?
function OnTriggerEnter (other : Collider) {
if (other.isTrigger)
return;
if (other.gameObject.tag == triggerTag || triggerTag == "") {
enterSignals.SendSignals (this);
}
}
This always the issue that comes up when I try to analyse code. Tutorials are great but without a step by step breakdown, the beginner is faced with an uphill struggle all the time.
So any help would be much appreciated!
/Jason
function OnTriggerEnter (other : Collider) { //this function gets a variable called other, which is of type Collider
if (other.isTrigger) //is the isTrigger checkbox checked on the Collider we have collided with?
return; //jump out of the function return to wherever you came from. This exits the function
if (other.gameObject.tag == triggerTag || triggerTag == "") { //is the tag of the object we have collided with the same as our triggerTag variable OR is the triggerTag variable an empty string?
enterSignals.SendSignals (this); //call the SendSignals function of the enterSignals variable. We provide the function with an instance of this, where this is the current class (or file) we are working in.
}
}
hope i made it a bit clearer…
Thank you! It does make it a lot clearer. It’s from the Angry Bots demo.
Perhaps you or someone else can now help me understand the logic of it. This script is attached to a gameObject which actually a sliding door. It’s from the Angry Bots demo. The gameObject has a box collider on it that is a trigger.
This function is looking at what “other” collider comes into contact with the sliding door box collider trigger, but first it checks to see if that “other” collider is a trigger, and if it is, the function is not applied (return). I don’t see how it could be possible for another “trigger” collider to come into contact with the doors box collider, because (1) the only things moving around are things that have colliders which aren’t triggers, such as the player, and (2) line 9 checks to see if it’s the player entering anyway. (triggerTag variable is “Player”),
So, on line 9, it checks “other”'s tag to see if it is indeed “Player” (which is the only bit that makes sense to me), but then it also checks to see if that tag is an empty string, using an OR operator. Why allow for that seemingly impossible event of an empty tag? The only colliders moving around are “Player” and a bunch of bots, each of which have their own tags like “enemySpider” etc.
If I’d written this fucntion, I would have simply had this line, and nothing more:
If (other.gameObject.tag == triggerTag) enterSignals.SendSignals
I don’t see the need for the other checking. But then again, I’m pretty ignorant when it comes to scripting, being a newbie. So I want to know what I’m missing…
Can anyone help?
I guess ( I never really played with the angrybots demo) that this is all done to make sure that no unwanted opening/closing of the door happens. Perhaps the player or enemies have got a collider that is set to trigger. Perhaps the developer thought it would be nice to check these things to make sure no errors would pop up… i don’t really know. What i can tell you is that, when working in a team, it is always best to do these kind of checks. If a level designer decides that there needs to be another gameobject in front of the door with a collider that is set to trigger then there would be a collision. Perhaps the level designer forgot to fill the triggerTag variable in the inspector and complains that the script is not working correctly.
Don’t know, but some extra checks happen in code very often just to make sure that no error’s appear and that the scripts usage is made easy for whoever needs to use it.
Thanks. Enough to understand WHAT, the WHY can wait, I suppose.