camera switch ????

hey guys i have looked for what i wanted but coudnot find it so i will ask you guys

i want to make a java script that alows me to

yield ForSeconds (10); than camera 2. witch is also names camera 2 has to be viewed

so that i switch afther 10 secs to camera 2

and that multiple times so i can make an mission intro movie becouse i wil go form cam 2 to cam 3 and 4 5 6 7

i hope you know what i need so coud you explain it to me how to make this

thank you for you time greatz wezz

Do you want a jump-cut or interpolation (smooth transition)?

I hope he was looking for a jump-cut, because that's what I gave him. :)

2 Answers

2
var waitTime = 10;
var numberOfCameras = 7; 

function Start() {        
    for (var i = 0; i<=numberOfCameras;i++) {
        var oldCamera : GameObject = null;
        if (i!=1) var oldCamera = GameObject.Find("Camera"+(i-1));
        var newCamera = GameObject.Find("Camera"+i);

        if(oldCamera) oldCamera.active = false;
        newCamera.active = true;

        yield WaitForSeconds(waitTime);
    }
}

Copy the above script into a script, attach it to any gameObject, make sure your cameras are named (exactly) Camera1, Camera2, Camera3, etc., you should be set to go.

Please note that the above script is untested and may contain errors.

Are your cameras named Camera1, Camera2, exactly? They need to have the correct capitols and everything.

You could also create an array of cameras to hold references to them, and just step forward through it every few seconds or so... this way you could even reuse cameras more than once through the sequence, by simply adding them again in a new position in the array

(example in C#, because I'm not too sure of syntax on UnityScipt)

using UnityEngine;
using System.Collections;    

public class CameraCycler : MonoBehaviour
{

    public Camera[] cameras; //defined in editor

    private int currentCamera; // index of current camera
    private bool running; // will keep the camera cycle going as long as it's true

    void Start()
    {
        currentCamera = 0; // start at first camera
        running = true;    // let's get it going
        StartCoroutine(cycleCameras());
    }

    IEnumerator cycleCameras()
    {
        while (running)
        {    
            yield return new WaitForSeconds(10.0f); // wait 10 seconds

            cameras[currentCamera].enabled = false;  // switch off the camera we have now
            currentCamera = currentCamera + 1 % cameras.Length; // increment index by one (and loop back to 0 at last)
            cameras[currentCamera].enabled = true; // switch on the new camera
        }
    }

}

Mind that this assumes all cameras, except the first one, are off by default (meaning, disabled in the editor). You could easily set up a loop on Start() to disable all cameras except the first one, though...

By declaring the array as public, Unity will pick it up and create a property for this script in the inspector window. There, you can just set how many cameras you'll be using, and just drag n' drop them into the array.

Hope it helps

Cheers