Hello, I seem to be having trouble when my FPS controller enters the box collider on its first animated gate (door) it opens fine. But when it arrives at the 2nd gate (door) same as the first one, it opens up the first door again. The box colliders are part of the gateentrance1 prefab on both doors. I added 2 tags (gate1 and gate2) and assigned them properly. Clueless on what to do after searching the forums for possible different ways to open different doors. Thanks!
function OnTriggerEnter (gateentrance1 : Collider) {
var gate = gameObject.FindWithTag("gate1");
gate.animation.Play("gateopen");
}
function OnTriggerExit (gateentrance1:Collider) {
var gate = gameObject.FindWithTag("gate1");
gate.animation.Play("gateshut");
}
If your going to use FindWithTag, then make separate tags for your doors, this way when it sets the variable gate it will point to the different door.
Since this should be one script for all your doors, make a public string variable to reference your correct tag name for each separate door.
Also, unless you want to check what collider triggered the collision, for example to make sure it was only the player that entered the trigger, then you don’t have to pass the Collider parameter through the OnTriggerEnter/Exit calls.
Example:
var doorTagName : String; // i.e. set to "gatet1", "gate2", "gate3", etc.
function OnTriggerEnter() {
var gate = gameObject.FindWithTag(doorTagName);
gate.animation.Play("gateopen");
}
function OnTriggerExit() {
var gate = gameObject.FindWithTag(doorTagName);
gate.animation.Play("gateshut");
}
Now the problem is I need the string to search the tags so it can use the proper one and I am not sure how to do this.
Here is what the code looks like now modified
var gatesearch : String;
function OnTriggerEnter (other:Collider) {
var gate = gameObject.FindWithTag(gatesearch);
gate.animation.Play("gateopen");
}
function OnTriggerExit (other:Collider) {
var gate = gameObject.FindWithTag(gatesearch);
gate.animation.Play("gateshut");
}
I have 2 same prefabs one is tagged gate1 one is tagged gate2.
Maybe there is an easier way to go about this using the same script to open all the doors.
I figured it out after using some basic logic. I was attaching the script to the FPS Controller and was trying to find a way to search the tags. Finally I reversed the procedure by attaching the script to the different doors and naming the door tag in the string and voila it works! Thanks for pointing me in the right direction Raiden!