I wrote an answer for this ages ago, it never even got accepted (or voted, I accepted it myself, sigh).
So, from my answer on : Switching Several Cameras(HorrorGame) - Questions & Answers - Unity Discussions
So I have been thinking about this, and may have a flexible approach.
Each camera is a component or child of your trigger object. So tell the camera to enable when the trigger is entered. But then there is the problem of turning the old camera off. Just store a reference to the currently used camera, and when the trigger enables a new camera, then disable the stored one, then make sure to store a reference to the new current camera.
Here is some rough untested code to try and explain the theory. The idea then is no matter how many cameras there are, this should keep working (as long as the trigger volumes are not too close together). Adding or removing cameras and trigger volumes becomes fun, not a hassle with rewriting code =]
// – PLAYER SCRIPT –
#pragma strict
// FOR MULTIPLE CAMERAS
public var currentCam : Camera;
function Start()
{
// Enable the first camera
currentCam = Camera.allCameras[0];
// disable all other cameras
for ( var i:int = 1; i < Camera.allCameras.Length; i ++ )
{
Camera.allCameras*.enabled = false;*
// ----
// – TRIGGER SCRIPT –
#pragma strict
var thePlayerScript : PlayerScript;
var myCamera : Camera;
function Start()
{
// find myCamera (child or component of trigger object)
myCamera = transform.parent.FindChild( “Camera” ).GetComponent( Camera );
// find the Player, store reference to player script
// to use .enabled = false; when switching cameras
thePlayerScript = GameObject.FindWithTag( “Player” ).GetComponent( PlayerScript );
}
function OnTriggerEnter( other : Collider )
{
// disable current camera
thePlayerScript.currentCam.enabled = false;
// enable this camera
myCamera.enabled = true;
// assign this camera as the current camera
thePlayerScript.currentCam = myCamera;
Debug.Log( transform.parent.gameObject.name + " Triggered by " + other.gameObject.name );
}
Any feedback is most welcome =]
The theory works : http://www.alucardj.net16.net/unityanswers/ResEvilCameras.html