- Home /
Switching Cameras - Confusion with Two Code Samples
I have a cube that's a trigger, and this script is attached to it. The purpose of the script is that when the Box is triggered, it switches to the Camera linked to the script. When the trigger is exited, the camera needs to revert back. I tried to dynamically determine which camera is active at runtime to no success, but if I choose it at design time, it works. :/
If I change the 'oldCamera' property to a value I select within the Unity editor, the code works like a charm (entering the trigger switches the camera, and leaving the cube switches the camera back).
#pragma strict
var selectedCamera : Camera;
var cameraTarget: Transform;
private var cameraActive: byte;
var oldCamera: Camera;
function Start () {
cameraActive = 0;
}
function LateUpdate () {
if (cameraActive == 1 && selectedCamera != null && cameraTarget != null)
{
selectedCamera.transform.LookAt(cameraTarget);
}
}
function OnTriggerEnter()
{
if (selectedCamera != null)
{
cameraActive = 1;
for (var c in Camera.allCameras) {
c.active = false;
}
selectedCamera.active = true;
}
}
function OnTriggerExit()
{
if (selectedCamera != null)
{
selectedCamera.active = false;
oldCamera.active = true;
cameraActive = 0;
}
Debug.Log("exitingTrigger");
}
Now here's the kicker. If I try to dynamically decide which camera to revert back to, nothing happens. This code below does not work, it just switches the camera when OnTriggerEnter is called. When the player leaves the trigger, the camera just freezes in place. It should - in theory - revert back to the other camera. At least that is how it would seem in my mind.
#pragma strict
var selectedCamera : Camera;
var cameraTarget: Transform;
private var cameraActive: byte;
private var oldCamera: Camera;
function Start () {
cameraActive = 0;
}
function LateUpdate () {
if (cameraActive == 1 && selectedCamera != null && cameraTarget != null)
{
selectedCamera.transform.LookAt(cameraTarget);
}
}
function OnTriggerEnter()
{
if (selectedCamera != null)
{
cameraActive = 1;
oldCamera = Camera.current;
for (var c in Camera.allCameras) {
c.active = false;
}
selectedCamera.active = true;
}
}
function OnTriggerExit()
{
if (selectedCamera != null)
{
selectedCamera.active = false;
oldCamera.active = true;
cameraActive = 0;
}
Debug.Log("exitingTrigger");
}
I also tried using oldCamera = Camera.main and checking if c.active==true then oldCamera = c in the for loop
Answer by hirenkacha · Apr 19, 2012 at 10:52 AM
the simple way to do this is,
use this C# script attached to cube
[code] public static bool InRegionFlag=false;
void OnTriggerEnter(Collider cube)
{
Debug.Log("Inside");
InRegionFlag=true;
}
void OnTriggerExit(Collider cube)
{
Debug.Log("OutSide");
InRegionFlag=false;
}
And in controller script use
public Camera sideCam; public Camera mainCam;
Start()
{
sideCam.camera.enabled=false;
mainCam.camera.enabled=true;
}
Update() {
if(camControll.InRegionFlag) {
sCam.camera.enabled=true;
mCam.camera.enabled=false;
}
else
{
sCam.camera.enabled=false;
mCam.camera.enabled=true;
}
[/code]
This fails miserably when I have more than two cameras. I might want 10 or 15 in a scene and this would cause nightmares for code maintenance
nop it should not. use 10 flags for 10 cameras. Enable the camera whose flag is true rest should be disabled.
Answer by ransomink · Mar 23, 2014 at 10:25 PM
This is an easy script that will switch the camera upon entering the trigger and revert back to the previous camera when exiting the trigger. Attach this to the trigger GameObject.
// INSPECTOR VARIABLES //
public var switchToCamera : Camera;// the camera to switch to (when trigger is entered)
// PRIVATE VARIABLES //
private var currentCamera : Camera;// the current camera being rendered
private var previousCamera : Camera;// the previous camera that 'was' being rendered (the current camera before entering the trigger).
// when the trigger is entered - switch cameras
function OnTriggerEnter ( col : Collider )
{
currentCamera = Camera.current;
previousCamera = currentCamera;
currentCamera.enabled = false;
currentCamera = switchToCamera;
currentCamera.enabled = true;
}
// when the trigger is exited - revert back to previous camera
function OnTriggerExit ( col : Collider )
{
currentCamera.enabled = false;
currentCamera = previousCamera;
currentCamera.enabled = true;
previousCamera = null;
}
You can change the currentCamera and previousCamera from private to public if you wish to see the camera status update inside the inspector.
Your answer
Follow this Question
Related Questions
Camera switching : C# onTriggerEnter not working 1 Answer
Camera switch after specific time 1 Answer
Camera switching by trigger 2 Answers
Play anim, javascript 1 Answer
Switching cameras (JavaScript) 0 Answers