- Home /
Camera switching by trigger
How do i make a camera switch between another camera bye netering a room. I have tried different scripts but i havent got them to work.
Is there a small code that, when i have a box with "Is Trigger on" and it's called Box1. And when i walk on Box1 the camera change from a camera to another.
I do have a script where i can change cameras by keys but i want the cameras to change automaticly.
Answer by Berenger · Jun 12, 2012 at 03:26 AM
First, you need to understand that there is to questions here. Not for us, but for you, so you get what you're doing. Much better than eating someone else's code. They are :
How do I detect the player entering a trigger then do something ?
How do I switch between two cameras.
For the first question, you need a script attached to the trigger that implement the function OnTriggerEnter. Inside, you need to test if the object entering is actually the player (usually, this is done with col.CompareTag("Player")), then you can do something. Like switching the cameras. Note that your trigger need to be correctly setup (size, position) and have the isTrigger box checked.
Second question, the switch. You need a reference of both camera. The first is active, the other inactive. You need to do this :
oldCam.gameObject.active = false;
newCam.gameObject.active = false;
Good luck !
That will make both cameras inactive, I believe you wanted to put:
oldCam.gameObject.active = false; // disable old camera
newCam.gameObject.active = true; // enable new camera
I think I always use the 'enabled' member variable too, no the 'active' variable.
Answer by zero84085 · Jul 18, 2012 at 07:43 AM
var Camera1 : Camera;
var Camera2 : Camera;
function OnTriggerEnter(){
Camera1.enabled = false;
Camera2.enabled = true;
}
function OnTriggerExit(){
Camera1.enabled = true;
Camera2.enabled = false;
}
Your answer
Follow this Question
Related Questions
Switching Cameras - Confusion with Two Code Samples 2 Answers
Camera switching : C# onTriggerEnter not working 1 Answer
How to set main camera? 3 Answers
enter a trigger to access other camera? 1 Answer
Active camera on Start 1 Answer