- Home /
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;
}
}
}
Apparently I put by comments right in the middle of my code. I didn't mean to do that. : /
Answer by centaurianmudpig · Apr 13, 2012 at 05:24 PM
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.
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.
Thank you. This worked perfectly for what I needed. As you can see, I used GetKeyDown instead.
Here's my code:
public class CameraSwitch : MonoBehaviour
{
public Camera Camera1;
public Camera Camera2;
// Start is called before the first frame update
void Start()
{
Camera1.enabled = true;
Camera2.enabled = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.C))
{
Camera1.enabled = !Camera1.enabled;
Camera2.enabled = !Camera2.enabled;
}
}
Answer by XtriMiles · Jul 04, 2014 at 03:32 AM
This works but I can see my body in the second camera, it looks like I'm teleporting
Answer by tdneren2 · Aug 24, 2014 at 06:04 PM
For any developer who reads this - camera change does not work after exporting to an exe-file
Answer by FuturescoGames · May 05, 2017 at 01:19 PM
Thanks a lot, good idea, but I have some problems with this so I change it to use SetActive and change it to use any number of cameras or any kind objects:
using UnityEngine;
public class ChangeObject : MonoBehaviour
{
public GameObject[] objects;
private int objectActual = 0;
void Start()
{
DisableObjects();
objects[0].gameObject.SetActive(true);
}
void Update()
{
if (Input.GetKeyUp(KeyCode.Return))
{
DisableObjects();
objectActual++;
if (objectActual >= objects.Length)
{
objectActual = 0;
}
objects[objectActual].gameObject.SetActive(true);
}
}
void DisableObjects()
{
foreach (GameObject obj in objects)
{
obj.gameObject.SetActive(false);
}
}
}
I hope it could be usefull
Your answer
Follow this Question
Related Questions
switching cameras C# 4 Answers
Camera switching : C# onTriggerEnter not working 1 Answer
Multiple Cars not working 1 Answer
Swapping Cameras Disables Keyboard Input 3 Answers
Distribute terrain in zones 3 Answers