x


Switch Camera on Input in C#

pragma

using UnityEngine; using System.Collections;

public class MenuPrototypes : MonoBehaviour {

bool cam = true;
bool cam2 = false;

void Start ()
{
    bool cam = true;
    bool cam2 = false;

I need help with switching from one camera to another by pressing 'Enter.' Here is my code. All the examples I see are in JavaScript but not C# : /. Any help is truly appreciated. I have been trying to figure this out for three days.

Please be as specific as possible as I am not good at scripting.... obviously. }

void SwapCamera ()
{
    if (Input.GetKeyUp(KeyCode.Return))    {           
    bool cam = false;
    bool cam2 = true; 
}

    }

}

more ▼

asked Apr 13, 2012 at 03:55 PM

latsushi gravatar image

latsushi
50 62 54 63

Apparently I put by comments right in the middle of my code. I didn't mean to do that. : /

Apr 13, 2012 at 02:27 PM latsushi
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

You are declaring the cam and cam2 variables locally, by using the bool type each time. Once you declare a variable, like you did at the top, you reference it without the type, i.e. cam = true;

I'll assume you have not referenced any camera's. Add these two lines, replacing cam and cam2 variables at the top:

public Camera camera;
public Camera camera2;

Then change your start routine:

void Start() {
camera.enabled = true;
camera2.enabled = false;
}

void Update() {
//This will toggle the enabled state of the two cameras between true and false each time
if (Input.GetKeyUp(KeyCode.Return)) {
camera.enabled = !camera.enabled;
camera2.enabled = !camera2.enabled;
}

Add your code to a GameObject, NOT the camera's. Attach your camera's to Camera and Camera2 against this GameObject in the inspector panel. Run the code and it should switch between the 2 camera's. Setup a scene so you can see it switch. This hasn't been tested, I wrote it straight in here but it should work.

more ▼

answered Apr 13, 2012 at 05:24 PM

centaurianmudpig gravatar image

centaurianmudpig
56 26 23 25

I am having some issues with switching the camera more than once. I set one camera to load I then click a gameObject and the camera changes. This camera has a GUI object that can be clicked to switch back to the first camera. So far so good. However if I click the gameObject again I cant go back into the second Camera.

I am using Culling layers and tags to show and hide objects but even without those masked layers I can only get the camera switches to work once.

Any ideas how to make this work where I can switch back and forth as many times as my heart desires?

Also thank you for the above test code. It was a great starting point.

Sep 20, 2013 at 04:37 PM Giantbean
(comments are locked)
10|3000 characters needed characters left

This solved the problem

Thank you

more ▼

answered Apr 15, 2012 at 07:01 AM

latsushi gravatar image

latsushi
50 62 54 63

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

This works but I can see my body in the second camera, it looks like I'm teleporting

more ▼

answered Jul 04 at 03:32 AM

XtriMiles gravatar image

XtriMiles
0

(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:

x9344
x4725
x330
x78

asked: Apr 13, 2012 at 03:55 PM

Seen: 6983 times

Last Updated: Jul 04 at 03:32 AM