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
1
Question by PurpleAshes · Mar 22, 2017 at 07:17 AM · switch cameras

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);
 
         }
     }
 }

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 PurpleAshes · Mar 22, 2017 at 12:18 AM 0
Share

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);
 
         }
     }
 }

3 Replies

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

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!

Comment
Add comment · Show 5 · 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 PurpleAshes · Mar 22, 2017 at 08:27 AM 0
Share

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.

avatar image herDev PurpleAshes · Mar 22, 2017 at 01:49 PM 2
Share

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.

avatar image PurpleAshes herDev · Mar 22, 2017 at 05:39 PM 0
Share

yea this worked ty man for your help appreciate it ^^

avatar image dfarjoun · Aug 07, 2017 at 08:41 AM 0
Share

This is perfect!! Thanks!!

avatar image Maverickthebest · Jan 27, 2019 at 08:26 PM 1
Share

what if i have 20 Cameras and want to twich ?? any idea?!

avatar image
0

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;
      }
  }

Comment
Add comment · Show 3 · 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 Maverickthebest · Jan 29, 2019 at 08:15 PM 0
Share

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

avatar image TSCaat · Oct 07, 2020 at 01:14 PM 0
Share

Hey, on line 28 I get an error, it says it can't devide by 0. What can I do to fix this?

avatar image Hellium TSCaat · Oct 07, 2020 at 01:15 PM 0
Share

Are you sure you have dragged & dropped Cameras in the inspector?

avatar image
0

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;
         }
     }
 }
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

8 People are following this question.

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

Related Questions

Drop down menu trigger'd cutscene 0 Answers

Camera Switched for Smartphone 0 Answers

movement disable when camera changes view 1 Answer

Camera Switch - Camera justs turns off, but does not switch 1 Answer

How would i change cameras when entering a store? 1 Answer


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