Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by anwserman · Apr 19, 2012 at 08:55 AM · camerajavascripttriggerswitch

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");
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image anwserman · Apr 19, 2012 at 09:10 AM 0
Share

I also tried using oldCamera = Camera.main and checking if c.active==true then oldCamera = c in the for loop

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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]

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image anwserman · Apr 19, 2012 at 04:59 PM 0
Share

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

avatar image hirenkacha · Apr 20, 2012 at 07:05 AM 0
Share

nop it should not. use 10 flags for 10 cameras. Enable the camera whose flag is true rest should be disabled.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges