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 FlashX · Dec 16, 2014 at 07:51 AM · c#cameraswitch

switching cameras C#

Hi all,

dumb question here but how can I switch between two cameras using c# in the most basic of ways?

I've found a bunch on JS but can seem to convert it over properly without errors.

thanks guys

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 taxvi · Dec 16, 2014 at 08:39 AM 0
Share

increase the depth of the new camera. or you can use enable/disable method.

4 Replies

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

Answer by FlashX · Dec 18, 2014 at 07:55 AM

ah figgured it out,

its this...

thanks to all who helped :)

 using UnityEngine;
 using System.Collections;
 
 public class CameraSwap : MonoBehaviour {
     
 
 
  public Camera camera1;
  public Camera camera2;
  public string whichCamera = "2";
 
 
 
 
 
     // Use this for initialization
     void Start () {
         //camera1.camera.enabled = !camera1.camera.enabled;
     }
     
     // Update is called once per frame
     void Update () {
 
         if(whichCamera == "1")
         {
             print("i'm in 1");
         camera1.camera.enabled = true;
         camera2.camera.enabled = false;
         }
 
 
         if(whichCamera == "2")
         {
             print("i'm in 2");
         camera1.camera.enabled = false;
         camera2.camera.enabled = true;
         }
     }
 }
 
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
avatar image
1

Answer by Fran-Martin · Dec 16, 2014 at 09:22 AM

Hi, start by attaching the script to a empty gameObject, and put the two cameras as children of this empty gameObject.

In the script, declare two variables of type camera. Get the reference to the scene cameras in the Start() method (using for example GetComponent method) and listen for the event you want to trigger the change in the update function. Within this event callback, set the attribute "enable" of the camera to true or false.

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
avatar image
0

Answer by Mmmpies · Dec 16, 2014 at 08:46 AM

What is it you're trying to do?

If you just want to swap the camera between two characters then just find that characters position and move the cameras transform.position to that character's with an offset on x y and z.

No real need for 2 cameras but there are situations where you still want the main camera to show in a small window but switch the main screen area to another camera.

Let us know what you're after and we can advise ways to get it.

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
avatar image
0

Answer by gribbly · Dec 16, 2014 at 08:41 AM

Create public variables for your cameras, like this:

 public Camera camera1;
 public Camera camera2;

Assign these cameras by dragging and dropping in the editor.

Then in your Update() you can do stuff like:

 if(Input.GetKeyDown(KeyCode.1)){
     camera1.camera.enabled = true;
 }
 if(Input.GetKeyDown(KeyCode.2)){
     camera2.camera.enabled = true;
 }

The most recently enabled camera will be the active camera.

Comment
Add comment · Show 4 · 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 FlashX · Dec 18, 2014 at 05:58 AM 0
Share

ah ok, so ive only been able to get round to this nowish but i'm still running into troubles, can anyone identify what i'm doing wrong here in C#?

my cameras names are "cam1" and "cam2" in my scene

 using UnityEngine;
 using System.Collections;
 
 public class CameraSwap : $$anonymous$$onoBehaviour {
     
 
     GameObject whichCamera;
 
  public Camera camera1;
  public Camera camera2;
 
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         whichCamera = GameObject.Find("cam1");
         camera1 = whichCamera.GetComponent<Camera>();
         whichCamera.camera.enabled = true;
     }
 }
 

thanks again

avatar image zaid87 · Dec 18, 2014 at 06:17 AM 1
Share

You should set the cam1 and cam2 to the camera1 and camera2 ins$$anonymous$$d, either by using editor drag & drop (probably better) or GameObject.Find(). And in Start & Update function, try:

 void Start(){
      //$$anonymous$$ake sure only one camera is active at a time
      camera2.camera.enabled = !camera2.camera.enabled;
 }
 
 void Update(){
     //switch the status of the camera
    camera1.camera.enabled = !camera1.camera.enabled;
    camera2.camera.enabled = !camera2.camera.enabled;
 }

You might want to set an IF statement inside Update though, since with just this, the camera might keep switching non-stop.

avatar image FlashX · Dec 18, 2014 at 07:01 AM 0
Share

thank you so much Zaid87,

not sure I can get it to work though, what am I doing wrong?

 using UnityEngine;
 using System.Collections;
 
 public class CameraSwap : $$anonymous$$onoBehaviour {
     
 
 
  public Camera camera1;
  public Camera camera2;
  public string whichCamera = "1";

 
 
 
 
     // Use this for initialization
     void Start () {
     camera1.camera.enabled = !camera1.camera.enabled;
     }
     
     // Update is called once per frame
     void Update () {
 
         if (whichCamera == "1")
         {
             print("i'm in 1");
         camera1.camera.enabled = !camera1.camera.enabled;
         }
 
 
         if (whichCamera == "2")
         {
             print("i'm in 2");
         camera2.camera.enabled = !camera2.camera.enabled;
         }
     }
 }
 
avatar image zaid87 · Dec 18, 2014 at 07:18 AM 0
Share

You forgot to change the value of "whichCamera". So if you want to continue with your code, just change it to

 void Update () {
         
         if (whichCamera == "1")
         {
             print("i'm in 1");
             camera1.camera.enabled = !camera1.camera.enabled;
             whichCamera = "2";
         }
         
         
         if (whichCamera == "2")
         {
             print("i'm in 2");
             camera2.camera.enabled = !camera2.camera.enabled;
             whichCamera = "1";
         }
     }

Again, this will cause the camera to keep switching non-stop every frame, so I suggest you set a condition for it.

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

32 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

Related Questions

Camera switching : C# onTriggerEnter not working 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Switch Camera on Input in C# 3 Answers

Switching between cameras by C# with animation. 3 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