- Home /
Trigger when 3 triggers collide.
Hi All,
I'm trying to do something really simple, trying to get something to happen when 3 or more triggers collide.
Let's say I have a main trigger A, and then 2 other triggers B and C. If B collides with A alone, nothing happens. If C collides with A alone, nothing happens. If B & C both collide with A (at the same time or one after the other, as long as they are colliding together at some point), I would like this to make something happen.
So I tag both of the colliders and try this script (which I put on A) but it doesn't work...
void OnTriggerEnter(Collider other) {
{
if (( other.gameObject.tag = "B") && ( other.gameObject.tag = "C"))
}
Do This();
}
Is this because "Collider other"is only reference to one object, and not two ?
Thanks !
Hyper
Answer by carrollh · Feb 05, 2015 at 02:35 PM
Try this instead. (I fixed your curly braces and boolean checks).
bool collidingB = false;
bool collidingC = false;
void OnTriggerEnter(Collider other) {
if ( other.gameObject.tag == "B" ) collidingB = true;
else if( other.gameObject.tag == "C" ) collidingC = true;
if( collidingB && collidingC ) {
Do This();
}
}
void OnTriggerExit(Collider other) {
if(other.gameObject.tag == "B") collidingB = false;
else if(other.gameObject.tag == "C") collidingC = false;
}
They hit it before my first edit. I was just fixing the typos in the question. $$anonymous$$y edit actually answered the root problem (after clearing the syntax errors of course ;)
Seems like a good answer ! Thanks a lot. Gonna try it out now and I'll let you know :) Btw, I don't think boolean works does it ? Thought it was just bool...
Also... What's the reason why my example doesn't work ? :) I could just move on, but I like to understand why it doesn't work at all.
You're right. I can't keep them straight between C#, js, and java. And I'm not in an IDE so no backup :P
Edited my script. Thanks. Let me know how it goes.