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
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
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.
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.
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.
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;
}
}
}