Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 SwagGamez · Jan 03, 2016 at 02:50 PM · cameraswitchcamerasswitching

Multiple Camera Switching

Ok I have a script here that works perfectly but I want to use one button to switch cameras (Camera1, Camera2 ...Camera5). I am using a ps3 controller and I want to use my Triangle button (joystick button 3) using the Input.GetAxis method which is called "Camera". The mirrors are minimaps attached to Camera1 and Camera2 Can anyone help? Here is the original script that works and my attempt to use Input.GetAxis script:

Original script:

  var mirrorOne : GameObject;
  var mirrorTwo : GameObject;


   function Start ()
 {
 camSwap(1);
 mirrorOne.GetComponent("Camera").enabled = true;
 mirrorTwo.GetComponent("Camera").enabled = false;
 }

   function Update () 
 {
 if(Input.GetKey("1"))
     {
       camSwap(1);
       mirrorOne.GetComponent("Camera").enabled = true;
     mirrorTwo.GetComponent("Camera").enabled = false;
      }

  if(Input.GetKey("2"))
      {
       camSwap(2);
       mirrorOne.GetComponent("Camera").enabled = false;
     mirrorTwo.GetComponent("Camera").enabled = true;
      }

  if(Input.GetKey("3"))
      {
       camSwap(3);
       mirrorOne.GetComponent("Camera").enabled = false;
     mirrorTwo.GetComponent("Camera").enabled = false;
      }

  if(Input.GetKey("4"))
      {
       camSwap(4);
       mirrorOne.GetComponent("Camera").enabled = false;
     mirrorTwo.GetComponent("Camera").enabled = false;
      }

  if(Input.GetKey("5"))
      {
       camSwap(5);
       mirrorOne.GetComponent("Camera").enabled = false;
     mirrorTwo.GetComponent("Camera").enabled = false;
      }
 }
 
   function camSwap(currentCam : int)
 {
  var cameras = GameObject.FindGameObjectsWithTag("MainCamera");
 
  for (var cams : GameObject in cameras)
      {
       cams.GetComponent(Camera).enabled = false;
       cams.GetComponent(AudioListener).enabled = false;
      }  
 
  var oneToUse : String = "Camera"+currentCam;
  gameObject.Find(oneToUse).GetComponent(Camera).enabled = true;
  gameObject.Find(oneToUse).GetComponent(AudioListener).enabled = true;
 }

My Attempt:

    var mirrorOne : GameObject;
    var mirrorTwo : GameObject;
    var cam : int;


     function Start ()
 {
     cam = 1;
 camSwap(1);
 mirrorOne.GetComponent("Camera").enabled = true;
 mirrorTwo.GetComponent("Camera").enabled = false;
 }

    function Update () 
 {
 if(Input.GetAxis("Camera") && cam ==1)
     {
             cam = 2;
       camSwap(2);
       mirrorOne.GetComponent("Camera").enabled = true;
     mirrorTwo.GetComponent("Camera").enabled = false;
      }

  if(Input.GetAxis("Camera") && cam ==2)
     {
             cam = 3;
       camSwap(3);
       mirrorOne.GetComponent("Camera").enabled = false;
     mirrorTwo.GetComponent("Camera").enabled = true;
      }

  if(Input.GetAxis("Camera") && cam ==3)
     {
             cam = 4;
       camSwap(4);
       mirrorOne.GetComponent("Camera").enabled = false;
     mirrorTwo.GetComponent("Camera").enabled = false;
      }

  if(Input.GetAxis("Camera") && cam ==4)
     {
             cam = 5;
       camSwap(5);
       mirrorOne.GetComponent("Camera").enabled = false;
     mirrorTwo.GetComponent("Camera").enabled = false;
      }

  if(Input.GetAxis("Camera") && cam ==5)
     {
             cam = 1;
       camSwap(1);
       mirrorOne.GetComponent("Camera").enabled = true;
     mirrorTwo.GetComponent("Camera").enabled = false;
      }
 }
 
     function camSwap(currentCam : int)
 {
  var cameras = GameObject.FindGameObjectsWithTag("MainCamera");
 
  for (var cams : GameObject in cameras)
      {
       cams.GetComponent(Camera).enabled = false;
       cams.GetComponent(AudioListener).enabled = false;
      }  
 
  var oneToUse : String = "Camera"+currentCam;
  gameObject.Find(oneToUse).GetComponent(Camera).enabled = true;
  gameObject.Find(oneToUse).GetComponent(AudioListener).enabled = true;
 }
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 SwagGamez · Jan 04, 2016 at 01:10 PM 0
Share

O$$anonymous$$ now this is pissing me off!!!

1) The cameras only cycle if there is 1 or no mirrors dragged into the inspector but I get an error saying that I am missing the mirror variable/s, which makes perfect sense to me.

2) But If I add both mirrors, the mirrors don't activate/deactivate and the cameras cycle from 1 through 5 and back to 1, with camera1's view never changing but the console shows the rapid cycle (1,2,3,4,5,1).

3) The mirrors only work if all the cameras are manually disabled in the hierarchy but obviously I have no cameras or listeners.

Here is the script:

    import System.Collections.Generic;
    import System.Linq;

    var mirrorOne : GameObject;
    var mirrorTwo : GameObject;
    var cam : int;

     function Start ()
 {
 Debug.Log("Cam 1");
 cam = 1;
 camSwap(1);
 mirrorOne.SetActive (true);
 mirrorTwo.SetActive (false);
 }

     function Update () 
 {
 if(Input.GetButtonDown("Camera") && cam == 1) 
     {
     Debug.Log("Cam 2");
     cam = 2;
       camSwap(2);
       mirrorOne.SetActive (false);
     mirrorTwo.SetActive (true);
      }

  if(Input.GetButtonDown("Camera") && cam == 2) 
     {
     Debug.Log("Cam 3");
     cam = 3;
       camSwap(3);
       mirrorOne.SetActive (false);
     mirrorTwo.SetActive (false);
      }

  if(Input.GetButtonDown("Camera") && cam == 3) 
     {
     Debug.Log("Cam 4");
     cam = 4;
       camSwap(4);
       mirrorOne.SetActive (false);
     mirrorTwo.SetActive (false);
      }

  if(Input.GetButtonDown("Camera") && cam == 4) 
     {
     Debug.Log("Cam 5");
     cam = 5;
       camSwap(5);
       mirrorOne.SetActive (false);
     mirrorTwo.SetActive (false);
      }

  if(Input.GetButtonDown("Camera") && cam == 5) 
     {
     Debug.Log("Cam 1");
     cam = 1;
       camSwap(1);
       mirrorOne.SetActive (true);
     mirrorTwo.SetActive (false);
      }
 }
 
     function camSwap(currentCam : int)
 {
  var cameras = GameObject.FindGameObjectsWithTag("$$anonymous$$ainCamera");
 
  for (var cams : GameObject in cameras)
      {
       cams.GetComponent(Camera).enabled = false;
       cams.GetComponent(AudioListener).enabled = false;
      }  
 
  var oneToUse : String = "Camera"+currentCam;
  gameObject.Find(oneToUse).GetComponent(Camera).enabled = true;
  gameObject.Find(oneToUse).GetComponent(AudioListener).enabled = true;
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SwagGamez · Jan 05, 2016 at 08:21 AM

Ok I figured it out. After the first if-statement, in the Update function, the rest of the ifs need to be else-if-statements. Works perfect now. Thanx for your help though fellas.

Comment
Add comment · Show 1 · 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 darkhog · Apr 23, 2016 at 02:46 PM 0
Share

Glad you've figured it out. $$anonymous$$ind if I use that script?

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

43 People are following this question.

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

Related Questions

Camera Switching or Scene Changes? 3 Answers

cameras switch doesn't work 1 Answer

Switching Between Cameras JavaScript 0 Answers

switch character 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