- Home /
Using switch/case with Colliders and Cameras
I'm trying to switch between cameras for different goal zones (Colliders with "is trigger" enabled) and my script doesn't seem to be doing the trick.
Sorry for the long block of code, here is what I'm using (js):
function OnTriggerEnter (other : Collider) {
var i : Collider;
switch(i) { case other.CompareTag("DrillSite"): camera1.enabled = false; camera2.enabled = false; camera3.enabled = false; camera4.enabled = true; camera5.enabled = false;
yield WaitForSeconds (16);
camera1.enabled = true;
camera2.enabled = false;
camera3.enabled = false;
camera4.enabled = false;
camera5.enabled = false;
break;
case other.CompareTag("DeathRay"): camera1.enabled = false; camera2.enabled = false; camera3.enabled = true; camera4.enabled = false; camera5.enabled = false;
yield WaitForSeconds (16);
camera1.enabled = true;
camera2.enabled = false;
camera3.enabled = false;
camera4.enabled = false;
camera5.enabled = false;
break;
case other.CompareTag("PhotoShoot"):
camera1.enabled = false;
camera2.enabled = false;
camera3.enabled = false;
camera4.enabled = false;
camera5.enabled = true;
yield WaitForSeconds (16);
camera1.enabled = true;
camera2.enabled = false;
camera3.enabled = false;
camera4.enabled = false;
camera5.enabled = false;
break;
default:
camera1.enabled = true;
camera2.enabled = false;
camera3.enabled = false;
camera4.enabled = false;
camera5.enabled = false;
yield WaitForSeconds (16);
camera1.enabled = true;
camera2.enabled = false;
camera3.enabled = false;
camera4.enabled = false;
camera5.enabled = false;
// executed if none of the above
}
}
Answer by Ivan 1 · Feb 03, 2011 at 09:12 PM
I haven't tested, but try this:
function OnTriggerEnter (other : Collider) {
switch(other.CompareTag) { case "DrillSite": camera1.enabled = false; camera2.enabled = false; camera3.enabled = false; camera4.enabled = true; camera5.enabled = false;
yield WaitForSeconds (16);
camera1.enabled = true; camera2.enabled = false; camera3.enabled = false; camera4.enabled = false; camera5.enabled = false;
break;
case "DeathRay": camera1.enabled = false; camera2.enabled = false; camera3.enabled = true; camera4.enabled = false; camera5.enabled = false;
yield WaitForSeconds (16);
camera1.enabled = true; camera2.enabled = false; camera3.enabled = false; camera4.enabled = false; camera5.enabled = false;
break;
case "PhotoShoot": camera1.enabled = false; camera2.enabled = false; camera3.enabled = false; camera4.enabled = false; camera5.enabled = true;
yield WaitForSeconds (16);
camera1.enabled = true; camera2.enabled = false; camera3.enabled = false; camera4.enabled = false; camera5.enabled = false; break;
default:
camera1.enabled = true; camera2.enabled = false; camera3.enabled = false; camera4.enabled = false; camera5.enabled = false;
yield WaitForSeconds (16);
camera1.enabled = true; camera2.enabled = false; camera3.enabled = false; camera4.enabled = false; camera5.enabled = false;
// executed if none of the above
}
}
Answer by Molix · Feb 03, 2011 at 07:06 PM
It looks like you're using the switch/case statement incorrectly. First of all, "i" is uninitialized -- you declare it and then on the next line use it before actually assigning anything to it. Second, the case items are booleans (CompareTag returns a boolean) and "i" is a Collider; even if "i" was set, they'd never be equal.
You'd probably have better luck with just if..else if (and get rid of "i", it does nothing).
Answer by LANDO · Feb 04, 2011 at 05:09 PM
Thank you Ivan for your code, and Molix for your advice. I've finally been able to get it to work with just a slight modification of the code Ivan provided: (js)
function OnTriggerEnter (other : Collider) {
switch(other.tag) { case "DrillSite": camera1.enabled = false; camera2.enabled = false; camera3.enabled = false; camera4.enabled = true; camera5.enabled = false;
yield WaitForSeconds (16);
camera1.enabled = true; camera2.enabled = false; camera3.enabled = false; camera4.enabled = false; camera5.enabled = false;
break;
The only change I made was using (other.tag) rather than (other.CompareTag).