- Home /
switching cameras C#
Hi all,
dumb question here but how can I switch between two cameras using c# in the most basic of ways?
I've found a bunch on JS but can seem to convert it over properly without errors.
thanks guys
increase the depth of the new camera. or you can use enable/disable method.
Answer by FlashX · Dec 18, 2014 at 07:55 AM
ah figgured it out,
its this...
thanks to all who helped :)
using UnityEngine;
using System.Collections;
public class CameraSwap : MonoBehaviour {
public Camera camera1;
public Camera camera2;
public string whichCamera = "2";
// Use this for initialization
void Start () {
//camera1.camera.enabled = !camera1.camera.enabled;
}
// Update is called once per frame
void Update () {
if(whichCamera == "1")
{
print("i'm in 1");
camera1.camera.enabled = true;
camera2.camera.enabled = false;
}
if(whichCamera == "2")
{
print("i'm in 2");
camera1.camera.enabled = false;
camera2.camera.enabled = true;
}
}
}
Answer by Fran-Martin · Dec 16, 2014 at 09:22 AM
Hi, start by attaching the script to a empty gameObject, and put the two cameras as children of this empty gameObject.
In the script, declare two variables of type camera. Get the reference to the scene cameras in the Start() method (using for example GetComponent method) and listen for the event you want to trigger the change in the update function. Within this event callback, set the attribute "enable" of the camera to true or false.
Answer by Mmmpies · Dec 16, 2014 at 08:46 AM
What is it you're trying to do?
If you just want to swap the camera between two characters then just find that characters position and move the cameras transform.position to that character's with an offset on x y and z.
No real need for 2 cameras but there are situations where you still want the main camera to show in a small window but switch the main screen area to another camera.
Let us know what you're after and we can advise ways to get it.
Answer by gribbly · Dec 16, 2014 at 08:41 AM
Create public variables for your cameras, like this:
public Camera camera1;
public Camera camera2;
Assign these cameras by dragging and dropping in the editor.
Then in your Update() you can do stuff like:
if(Input.GetKeyDown(KeyCode.1)){
camera1.camera.enabled = true;
}
if(Input.GetKeyDown(KeyCode.2)){
camera2.camera.enabled = true;
}
The most recently enabled camera will be the active camera.
ah ok, so ive only been able to get round to this nowish but i'm still running into troubles, can anyone identify what i'm doing wrong here in C#?
my cameras names are "cam1" and "cam2" in my scene
using UnityEngine;
using System.Collections;
public class CameraSwap : $$anonymous$$onoBehaviour {
GameObject whichCamera;
public Camera camera1;
public Camera camera2;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
whichCamera = GameObject.Find("cam1");
camera1 = whichCamera.GetComponent<Camera>();
whichCamera.camera.enabled = true;
}
}
thanks again
You should set the cam1 and cam2 to the camera1 and camera2 ins$$anonymous$$d, either by using editor drag & drop (probably better) or GameObject.Find(). And in Start & Update function, try:
void Start(){
//$$anonymous$$ake sure only one camera is active at a time
camera2.camera.enabled = !camera2.camera.enabled;
}
void Update(){
//switch the status of the camera
camera1.camera.enabled = !camera1.camera.enabled;
camera2.camera.enabled = !camera2.camera.enabled;
}
You might want to set an IF statement inside Update though, since with just this, the camera might keep switching non-stop.
thank you so much Zaid87,
not sure I can get it to work though, what am I doing wrong?
using UnityEngine;
using System.Collections;
public class CameraSwap : $$anonymous$$onoBehaviour {
public Camera camera1;
public Camera camera2;
public string whichCamera = "1";
// Use this for initialization
void Start () {
camera1.camera.enabled = !camera1.camera.enabled;
}
// Update is called once per frame
void Update () {
if (whichCamera == "1")
{
print("i'm in 1");
camera1.camera.enabled = !camera1.camera.enabled;
}
if (whichCamera == "2")
{
print("i'm in 2");
camera2.camera.enabled = !camera2.camera.enabled;
}
}
}
You forgot to change the value of "whichCamera". So if you want to continue with your code, just change it to
void Update () {
if (whichCamera == "1")
{
print("i'm in 1");
camera1.camera.enabled = !camera1.camera.enabled;
whichCamera = "2";
}
if (whichCamera == "2")
{
print("i'm in 2");
camera2.camera.enabled = !camera2.camera.enabled;
whichCamera = "1";
}
}
Again, this will cause the camera to keep switching non-stop every frame, so I suggest you set a condition for it.
Your answer
Follow this Question
Related Questions
Camera switching : C# onTriggerEnter not working 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Switch Camera on Input in C# 3 Answers