I want to have two different cameras in my game, one top down and one 3rd person, and I want them to be changeable when you press a key, or change an option in the GUI. Is it possible to select a different main camera or would I have to move the main camera?
You can do either, although it's probably simpler to switch between cameras.
To change the main camera, you can set the "active" properties of the camera gameobjects, or you can set the "enabled" properties of the camera components on the gameobjects. It's often preferable to use the enabled property, which is shown below:
Camera.main refers to the first enabled camera tagged with "MainCamera", so you can tag both cameras with this tag, and use a simple script to toggle between them like this:
var cam1 : Camera;
var cam2 : Camera;
function Start() {
cam1.enabled = true;
cam2.enabled = false;
}
function Update() {
if (Input.GetKeyDown(KeyCode.C)) {
cam1.enabled = !cam1.enabled;
cam2.enabled = !cam2.enabled;
}
}
Place this script on a gameobject in your scene (for example, your player, or a new empty gameobject), and drag references to both your cameras into the "cam1" and "cam2" variables respectively.
The static variable "Camera.main" will always refer to the currently active camera out of these two.
If you want to do this for many cameras, just use an array. Add an empty component in the inspector and attach this code. Then drag all cameras that you want to be able to cycle through to the array element "cameras"
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public Camera[] cameras;
private int currentCameraIndex;
// Use this for initialization
void Start () {
currentCameraIndex = 0;
//Turn all cameras off, except the first default one
for (int i=1; i<cameras.Length; i++)
{
cameras*.gameObject.SetActive(false);*
-
}*
-
//If any cameras were added to the controller, enable the first one*
-
if (cameras.Length>0)*
-
{*
-
cameras [0].gameObject.SetActive (true);*
-
Debug.Log ("Camera with name: " + cameras [0].camera.name + ", is now enabled");*
-
}*
-
}*
-
// Update is called once per frame*
-
void Update () {*
-
//If the c button is pressed, switch to the next camera*
-
//Set the camera at the current index to inactive, and set the next one in the array to active*
-
//When we reach the end of the camera array, move back to the beginning or the array.*
-
if (Input.GetKeyDown(KeyCode.C))*
-
{*
-
currentCameraIndex ++;*
-
Debug.Log ("C button has been pressed. Switching to the next camera");*
-
if (currentCameraIndex < cameras.Length)*
-
{*
-
cameras[currentCameraIndex-1].gameObject.SetActive(false);*
-
cameras[currentCameraIndex].gameObject.SetActive(true);*
-
Debug.Log ("Camera with name: " + cameras [currentCameraIndex].camera.name + ", is now enabled");*
-
}*
-
else*
-
{*
-
cameras[currentCameraIndex-1].gameObject.SetActive(false);*
-
currentCameraIndex = 0;*
-
cameras[currentCameraIndex].gameObject.SetActive(true);*
-
Debug.Log ("Camera with name: " + cameras [currentCameraIndex].camera.name + ", is now enabled");*
-
}*
-
}*
-
}*
}
Here’s how I did it with three cameras:
var cam1: Camera;
var cam2: Camera;
var cam3: Camera;
function Start() {
cam1.enabled = true;
cam2.enabled = false;
cam3.enabled = false;
}
function Update() {
if (Input.GetKeyDown(KeyCode.V) && (cam1.enabled == true || cam3.enabled == true)) {
cam1.enabled = false;
cam2.enabled = true;
cam3.enabled = false;
}
else if (Input.GetKeyDown(KeyCode.B) && (cam2.enabled == true || cam1.enabled == true)) {
cam1.enabled = false;
cam2.enabled = false;
cam3.enabled = true;
}
else if (Input.GetKeyDown(KeyCode.C) && (cam2.enabled == true || cam3.enabled == true)) {
cam1.enabled = true;
cam2.enabled = false;
cam3.enabled = false;
}
}
This my way of doing it i know other people can do it in a much better way but i am beginner so please pardon me. The code goes like this
using UnityEngine;
public class PerspectiveChange : MonoBehaviour {
public GameObject cam1;
public GameObject cam2;
//You can do how much cams you want
public float perspective = 0f;
public float maxPerspective = 1f; //This is how many perspectives you want
void Update() {
if ( Input.GetKeyDown("v") ) {
if ( perspective == 0 ) {
cam1.SetActive(false);
cam2.SetActive(true);
perspective++;
}
else if ( perspective == 1 || perspective == maxPerspective ) {
cam1.SetActive(true);
cam2.SetActive(false);
perspective--;
}
}
}
}
And btw the code is all the way from using unityengine. And you have to add the player follower to both cams if you want to make the camera follow it the follow camera is
using UnityEngine;
public class CameraFollow : MonoBehaviour {
public Transform player;
public Vector3 offset;
// Update is called once per frame
void Update() {
transform.position = player.position + offset;
}
}
The offset is how much the camera will be offset from the player or the object you want to track. And i recommend to switch the cam in game and then set the camera offset for the different cams(Cause its just easy). And remember you have to disable all other cams other than the main cam
I know that this has been answered a while ago, but I stumbled across it and decided to share my insight into another solution for swapping cameras in scenes. One thing to note is that you could trade up explicitly defining the variables for the cams and buttons with arrays to increase performance, but I decided that doing it this way made for more readable code.
Note: This might be best implemented in a script attached to an empty game object in the scene and named CameraManager.cs
// Imports
using System.Collection.Generic;
// Define Cams
public Camera lobbyCam;
public Camera playerCam;
// Define Buttons
public string lobbyCamButton;
public string playerCamButton;
//Define List of Cameras
private List<Camera> cameras;
void Start(){
// Add inspector Cameras here
cameras.Add(lobbyCam);
cameras.Add(playerCam);
}
void Update(){
// Check for input and swap accordingly
if(Input.GetButtonDown(lobbyCamButton){
SwapCamera(lobbyCam);
}
if(Input.GetButtonDown(playerCamButton){
SwapCamera(playerCam)
}
}
public void SwapCamera(Camera cam){
// performance tradeoff
foreach(Camera c in cameras){
c.enabled = false;
}
cam.enabled = true;
}
whenever i try to type var, it completes it with something else. Am i missing a namespace or something?
thanx!
How do I make it so when an object that is attached to camera is turned off, then when it turns on it appears where I placed in attached to the camera.
Example, if the player has a gun attached to the camera, and I want it set to false but then set to true, I want the gun to appear on the camera and somewhere else in the game.
here is an easy solution that i used to change camera pov-s in unity. I am just started coding in c# so if it is bad tell me and give me tips :))
public class FollowPlayerV2 : MonoBehaviour
{
//place the cameras in the game object.
public GameObject cam1;
public GameObject cam2;
private float a = 0f;
void Start()
{
}
// Update is called once per frame
void Update()
{
//cam1 should be the main camera!
if(Input.GetKeyDown(KeyCode.C)){
a++;
}
if(a == 1){
cam1.SetActive(false);
cam2.SetActive(true);
}else if(a == 0){
cam1.SetActive(true);
cam2.SetActive(false);
}
if(a == 2){
a=0;
}
}
}
so what I did here was that I created a float, and I made it so, that when I press “C” on my keyboard the float “a” is increased by 1, and rest you can see what I did in the code using if-s (good thing is that I somehow managed to make it so if you hold c it wont start twiching and switching camera pov-s all the time :))))))))))))). dont forget to apply this code to a random empty object or a player. rate my code from 0 to 10.