Switching cameras

Hello Unity forums,

I’ve been working on this for the longest time, browsed the forums for a solution and still came up with nothing. It might sound very simple but it seems pretty complicated. I want to be able to have a camera (or POV) to move across the x axis for a certain distance or period of time, then switch to another camera once it’s done. In other words: I want the camera to move for a bit, then switch to another camera and let that move, then switch again. I’ve been having a lot of difficulty with it.

I thought Camera.Current would do the trick but it is readonly:

Maybe you could just enable the new camera and disable the old one and then switch back the same way.

Yeah I checked out Camera current and it seemed like it would be a pain to use. And I’ve been working on the enabling and disabling situation but the reason I cant figure that out is because I need to find a way to track how far the camera goes. I’d like it to go a certain distance or time, then switch. Any good time or distance variables that I could use? I also use the transform.Translate variable which seems to move my camera too fast. :stuck_out_tongue:

Camera.current is get-only, so it cannot be used.

If you want an action to take place in a certain amount of time (like switching camera), you read about yield - there you will find examples of waiting for a certain amount of time.

If you want to switch camera based on where the player or camera or something else is located, use Colliders set to IsTrigger=True… and do your scripting inside the OnTriggerEnter() function.

transform.Translate is a function where you can send any values you want. Hence, you can get the velocity of you choice.

Okay so here’s my new script and apparently it doesn’t work. The camera seems to not want to move at all. :stuck_out_tongue:

function Update () {
//move the camera
transform.Translate(Vector3.right * Time.deltaTime);
//wait 5 seconds
yield WaitForSeconds(5);
//stop the camera 
transform.Translate(0, 0, 0);

}

I have not used yield and WaitForSeconds but if they work the way you use them, try this:

var movecam = true;
function Start()
{
//wait 5 seconds 
yield WaitForSeconds(5); 
//stop the camera 
transform.Translate(0, 0, 0); 
movecam=false;
}

function Update () { 
//move the camera 
if (movecam)
{
transform.Translate(Vector3.right * Time.deltaTime); 
}
}

[/code]

Awesome! Thank you so much it worked perfectly! :smile: