- Home /
Alternating Two Settings with One OnTriggerEnter Collider
Hey all - I have a quick question.
I am doing some tests with switching fixed cameras with OnTriggerEnter. I am using a 3rd person point and click paradigm and am successfully switching the main camera/audio listener to a second main camera/audio listener when the 3rd person avatar hits the trigger.
My question is how would I switch BACK to the previous main camera/audio listener setting when I go through the same OnTriggerEnter collider again? Ultimately, I want to be able to alternate between two fixed camera settings infinitely based off of one OnTriggerEnter collider. Think Point & Click Adventure or "Resident Evil 1" style.
Here's the simple part that I scrounged together:
//This will switch your main camera (and audio listener) when you hit a trigger
//Make sure you have your second camera inactive when the scene starts
var camera1 : Camera;
var camera2 : Camera;
private var startCamera : int = 1;
function OnTriggerEnter(other : Collider)
{
if(other.CompareTag("Player"))
{
camera1.enabled = false;
camera1.gameObject.active = false;
camera2.enabled = true;
camera2.gameObject.active = true;
startCamera = 2;
}
}
Thanks a bunch!
Answer by ScroodgeM · Aug 18, 2012 at 07:47 PM
camera1.enabled = !camera1.enabled; camera1.gameObject.active = !camera1.gameObject.active; camera2.enabled = !camera2.enabled; camera2.gameObject.active = !camera2.gameObject.active; startCamera = startCamera == 2 ? 1 : 2;
Seems like that almost works. It does turn off the 1st Camera/Audio Listener and tries to switch over to the other, but it actually turns off the camera component of the 2nd Camera. The Audio Listener and everything else seems to change over correctly. You can hear from the 2nd perspective but everything renders in black.
hmm... note that by default camera2 component should be off and camera1 on. my code example just changes state to inversed every time. if both cameras are enabled, on code executing both will be disabled.
Ah yes. I'm an idiot. I was thinking that if I had the maincamera disabled, it would make the camera component behave accordingly. I WAS WRONG!
This worked, thanks a lot, Scroodge. $$anonymous$$uch appreciated.
Your answer
![](https://koobas.hobune.stream/wayback/20220613075847im_/https://answers.unity.com/themes/thub/images/avi.jpg)