# Changing cameras

I have a questions about changing cameras randomly. I have a rndCam object, with three animations attached (Camera1, Camera2, Camera3).
The idea is to:

• For the opening shot, hold the camera for the length in seconds defined in InitalShot
• Pick a random length (in seconds) for a shot to last in ShotDuration. Upper and lower bounds defined by minShot and maxShot. So shotDuration = Time.time+random length of time.
• Once Time.time == shotDuration, change to a (new) random camera.

The code I wrote is:

``````function Update ()
{
var minShot=7;  // Minimum Length of Shot  in seconds
var maxShot=13; // Maximum Length of Shot in seconds
var initialShot=10; // Length of the initial camera shot
var totalCameras=3; // Total number of Cameras
var shotDuration=10; // some initial value

if (Time.time > initialShot) {    // wait for opening shot time
if (Time.time > shotDuration) {
var nextCam = Random.Range(1,totalCameras+1);
shotDuration = Time.time + Random.Range(minShot, maxShot+1);
switch (nextCam)
{
case 1:
animation.Play("Camera1");
break;
case 2:
animation.Play("Camera2");
break;
case 3:
animation.Play("Camera3");
break;
//			Case n:
//			animation.Play("CamN");
//			break
}

}
}
}
``````

This does not work, since each time Update() is called, shotDuration is initialized to 10. if I remove the initialization, then the compiler horks, since ShotDuration is not defined for the first if compare.

What am I missing?

declare your vars outside the update function

Thanks! One question. Do you mean here:

``````   var minShot=7;  // Minimum Length of Shot  in seconds
var maxShot=13; // Maximum Length of Shot in seconds
var initialShot=10; // Length of the initial camera shot
var totalCameras=3; // Total number of Cameras
var shotDuration=10; // some initial value
function Update ()
{
......
}
``````

or as global statics?

yes like there. this will also allow you to change the values in the inspector with out having to edit the script. (note: if game view is in play, the values will change back to the last when you stop play)