- Home /
How can I switch between two cameras ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CutsceneManager : MonoBehaviour
{
public GameObject player;
public GameObject npc;
public GameObject doorEnteredID;
public DoorsLockManager doorlockmanager;
public string distance;
public string state;
public Camera playerCamera;
public Camera mainCamera;
private bool playerEntered = false;
private float distOfID;
private AudioListener playerAudioListener;
private AudioListener mainCameraAudioListener;
// Start is called before the first frame update
void Start()
{
playerAudioListener = playerCamera.GetComponent<AudioListener>();
mainCameraAudioListener = mainCamera.GetComponent<AudioListener>();
}
// Update is called once per frame
void Update()
{
float dist = Vector3.Distance(npc.transform.position, player.transform.position);
if (dist < 12)
{
distance = dist.ToString();
if (doorlockmanager.Doors[7].doorLockState == false)
{
// Shooting scene
state = "Shooting scene";
playerCamera.gameObject.SetActive(false);
playerAudioListener.enabled = false;
}
else
{
// Magic scene
state = "Magic scene";
playerCamera.gameObject.SetActive(false);
playerAudioListener.enabled = false;
mainCamera.gameObject.SetActive(true);
mainCameraAudioListener.enabled = true;
}
}
else
{
if (doorlockmanager.Doors[7].doorLockState == true)
{
// Magic scene
state = "Magic scene";
playerCamera.gameObject.SetActive(false);
playerAudioListener.enabled = false;
mainCamera.gameObject.SetActive(true);
mainCameraAudioListener.enabled = true;
}
}
if (doorEnteredID != null)
{
distOfID = Vector3.Distance(player.transform.position, doorEnteredID.transform.position);
}
if (distOfID < 1)
{
playerEntered = true;
doorlockmanager.Doors[7].doorLockState = true;
Destroy(doorEnteredID);
}
}
}
I want to switch between the two cameras and the audiolisteners. But it's disabling/enabling the cameras/s GameObject and not the Camera it self.
For example screenshot of the player camera : I want it to disable/enable the Camera but instead it's disabling the top FPSCamera ( The camera gameobject ).
Answer by LocalsOnlyCoop · Apr 14, 2019 at 11:24 PM
The simplest solution:
playerCamera.enabled = false;
Which disables the camera component.
Instead of
playerCamera.gameObject.SetActive(false);
which disables the entire game object..
I am curious why you wouldn't want to disable the game object though. That would make your code a lot simpler. You could just hold a reference to the game objects that hold the audio listener and the camera and turn them both off with one call
public GameObject mainCameraGO;
//Your main camera object would have both the camera and the audio listner.
mainCameraGO.SetActive(false);
Alternatively, you could use a UnityEvent to hook your game objects and then you could manage what turns on and off when in the inspector ins$$anonymous$$d of from code.
public UnityEvent onDoorLocked;
public UnityEvent onDoorUnlocked;
if(doorIsLocked)
{
onDoorLocked.Invoke();
} else
{
onDoorUnlocked.Invoke();
}
Just a thought.
Your answer
Follow this Question
Related Questions
How can i track/update the lastPosition of a camera when switching between cameras ? 0 Answers
How do I tilt my camera when I'm doing a certain action 1 Answer
Why the player is keep moving sometimes out of the grid area ? 0 Answers
Script for a hue rainbow scroll on the material 1 Answer
Multiple gameobjects but only one is screen wrapping. 0 Answers