- Home /
switch between Cameras in C#
hello i know this question been asked a lot but i couldn't find a clear answer and i really don't want to change this script so idk what's wrong with this script am using [it goes find after selecting the cameras but when i switch to 3rd person it goes back to first]
*sorry kind of learning here
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangPOV : MonoBehaviour
{
public Transform Player;
public Camera FirstPersonCam;
public Camera ThirdPersonCam;
public KeyCode Key;
void Start()
{
ThirdPersonCam.gameObject.SetActive(false);
FirstPersonCam.gameObject.SetActive(true);
}
void Update()
{
if (Input.GetKeyDown(Key))
{
FirstPersonCam.gameObject.SetActive(false);
ThirdPersonCam.gameObject.SetActive(true);
}
else
{
FirstPersonCam.gameObject.SetActive(true);
ThirdPersonCam.gameObject.SetActive(false);
}
}
}
I've managed to fix it temporally with:
but the problem is i want it to be one key to switch between them not 2 and the other problem is Unity keep telling me there are 2 active cameras in the game
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangPOV : $$anonymous$$onoBehaviour
{
public Transform Player;
public Camera FirstPersonCam;
public Camera ThirdPersonCam;
public $$anonymous$$eyCode T$$anonymous$$ey;
public $$anonymous$$eyCode F$$anonymous$$ey;
void start()
{
// ThirdPersonCam.gameObject.SetActive(false);
// FirstPersonCam.gameObject.SetActive(true);
}
void Update()
{
if (Input.Get$$anonymous$$eyDown(T$$anonymous$$ey))
{
FirstPersonCam.gameObject.SetActive(false);
ThirdPersonCam.gameObject.SetActive(true);
}
if (Input.Get$$anonymous$$eyDown(F$$anonymous$$ey))
{
FirstPersonCam.gameObject.SetActive(true);
ThirdPersonCam.gameObject.SetActive(false);
}
}
}
Answer by herDev · Mar 22, 2017 at 07:31 AM
Hi, this should work for you:
public class ChangPOV : MonoBehaviour
{
public Transform Player;
public Camera FirstPersonCam, ThirdPersonCam;
public KeyCode TKey;
public bool camSwitch = false;
void Update()
{
if (Input.GetKeyDown(TKey))
{
camSwitch = !camSwitch;
FirstPersonCam.gameObject.SetActive(camSwitch);
ThirdPersonCam.gameObject.SetActive(!camSwitch);
}
}
}
Good luck with your project!
ty man but i keep receiving a massive number of messages saying:
There are 2 audio listeners in the scene. Please ensure there is always exactly one audio listener in the scene.
Hi, This is because you have more than one camera. Every-time you create a camera, an audio listener component is attached to a camera by default. Just remove one of the audio listeners from a camera in the Inspector window.
yea this worked ty man for your help appreciate it ^^
what if i have 20 Cameras and want to twich ?? any idea?!
Answer by Hellium · Jan 28, 2019 at 06:58 AM
In response to @Maverickthebest, a script to allow multiple cameras to be used (code not tested)
public class ChangCameras : MonoBehaviour
{
public Camera[] Cameras;
public KeyCode NextCameraKey;
public KeyCode PreviousCameraKey;
private int selectedCameraIndex;
void Start()
{
DisableCameras();
SelectCamera( 0 ) ;
}
void Update()
{
if (Input.GetKeyDown(NextCameraKey))
SelectNextCamera();
if (Input.GetKeyDown(PreviousCameraKey))
SelectPreviousCamera();
}
public void SelectNextCamera()
{
selectedCameraIndex = (selectedCameraIndex + 1) % Cameras.Length;
SelectCamera( selectedCameraIndex );
}
public void SelectPreviousCamera()
{
selectedCameraIndex = (selectedCameraIndex - 1 + Cameras.Length ) % Cameras.Length;
SelectCamera( selectedCameraIndex );
}
public void SelectCamera( int cameraIndex )
{
if( cameraIndex >= 0 && cameraIndex < Cameras.Length )
{
Cameras[selectedCameraIndex].enabled = false;
selectedCameraIndex = cameraIndex;
Cameras[selectedCameraIndex].enabled = true;
}
}
private void DisableCameras()
{
for( int i = 0 ; i < Cameras.Length ; i++ )
Cameras[i].enabled = false;
}
}
This is Perfekt! Thank you so much! And by the way i never knew that you can use $$anonymous$$eyCode as a Variable :) With a nice Dropdown $$anonymous$$enue in the Editor! Best Quick Time ever! Oh you only mised in the Start, after DisableCameras() the semicolon, but this was quick fixed :) Cheers
Hey, on line 28 I get an error, it says it can't devide by 0. What can I do to fix this?
Are you sure you have dragged & dropped Cameras in the inspector?
Answer by LiYining · Jul 19, 2020 at 11:48 AM
public class Cameras : MonoBehaviour
{
private Vector3 mainCameraOffset = new Vector3(0, 4, -6.5f);
private Vector3 leftCameraOffset = new Vector3(-1.7f, 0.9f, 0);
private Vector3 topCameraOffset = new Vector3(0, 300, 0);
public GameObject player;
public Camera[] cameras;
public int cameraIndex = 0;
// Start is called before the first frame update
void Start()
{
player = GameObject.Find("Player");
cameras[cameraIndex].gameObject.SetActive(true);
for (cameraIndex += 1; cameraIndex < cameras.Length; cameraIndex++)
{
cameras[cameraIndex].gameObject.SetActive(false);
}
cameraIndex = 0;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.C))
{
cameraIndex++;
if (cameraIndex == cameras.Length)
{
cameraIndex = 0;
cameras[cameras.Length - 1].gameObject.SetActive(false);
}
cameras[cameraIndex].gameObject.SetActive(true);
if (cameraIndex != 0)
{
cameras[cameraIndex - 1].gameObject.SetActive(false);
}
}
}
void LateUpdate()
{
switch (cameraIndex)
{
case 0:
Camera.main.transform.position = player.transform.position + mainCameraOffset;
break;
case 1:
Camera.main.transform.position = player.transform.position + leftCameraOffset;
break;
case 2:
Camera.main.transform.position = player.transform.position + topCameraOffset;
break;
default:
break;
}
}
}