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.

var Camera1 : Camera;

var Camera2 : Camera;

function OnTriggerEnter(){

Camera1.enabled = false;

Camera2.enabled = true;

}

function OnTriggerExit(){

Camera1.enabled = true;

Camera2.enabled = false;

}

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 !