x


Switching between cameras

Hi, I would like to switch between cameras depending on the position of my player position, how can I do this please?

Thanks a lot for your time.

more ▼

asked Apr 02 '10 at 11:16 AM

Dev gravatar image

Dev
47 1 1 3

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

You can activate and deactivate cameras if you can get a hold of the object that holds them.

//cameraObject is the gameObject which holds the camera

cameraObject.camera.active = false;

So If you had a script which kept track of all the cameras in your scene then you could switch cameras easily. For example here is a function which would go through an array of camera objects and turn a certain one on (based on the index sent to it).

var cameras : GameObject[]; 

function SelectCamera (index : int) {
for (var i : int=0 ;i<wcameras.length; i++) {
    // Activate the selected camera
    if (i == index){
        cameras[i].camera.active = true;
    // Deactivate all other cameras
    }else{
        cameras[i].camera.active = false;
    }

}

You could use this function by calling it with a message

BroadcastMessage ("SelectCamera", 1);

or by outright calling it

SelectCamera(1);

You just need to get hold of the instance of the script it's in

more ▼

answered Apr 02 '10 at 01:55 PM

Jason_DB gravatar image

Jason_DB
1.9k 4 14 36

(comments are locked)
10|3000 characters needed characters left

DastardlyBanana's answer would be more flexible, but i'll post my (very simple!) answer here anyway:

You could check for a collision with a collider and then enabled/disable your cameras, so:

var Camera1 : Camera;
var Camera2 : Camera;

function Start(){
    Camera1.enabled = true;
    Camera2.enabled = false;

}

function OnTriggerEnter (other : Collider) {

    if(other.gameObject.name == "switch_camera"){

        Camera1.enabled = false;
        Camera2.enabled = true;

    }
}

function OnTriggerExit (other : Collider) {

    if(other.gameObject.name == "switch_camera"){

        Camera1.enabled = true;
        Camera2.enabled = false;

    }
}

Camera1 and Camera2 are assigned in the editor, or you could assign them with code.

Cheers.

more ▼

answered Apr 02 '10 at 02:02 PM

straydogstrut gravatar image

straydogstrut
1.2k 29 38 60

(comments are locked)
10|3000 characters needed characters left

Thanks because I've just looked at your answer I had a lot o thngs to do but it's not as interresting as your answer thanks to you 2!!

more ▼

answered Apr 22 '10 at 09:19 AM

askerer gravatar image

askerer
1 2

Keep comments off of the answers area

Apr 22 '10 at 02:36 PM spinaljack
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3014
x237
x83

asked: Apr 02 '10 at 11:16 AM

Seen: 6642 times

Last Updated: Apr 13 '10 at 09:42 AM