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 /
  • Help Room /
avatar image
0
Question by jimm84 · Feb 26, 2017 at 12:04 PM · c#camerakeyboardswitchsimple

Simple Camera Switch Using C#

Hello, I'm new to the land of Unity and I'm trying to muddle my way through. I'm probably having issues due to inexperience using Unity and C#.

In very simple scene, I'm trying to make 2 camera's switch when a key is pressed on the board (C). Is there a simple method to achieve this using enabled = true?

Other notes that could be of use:

I have followed other vid tuts without success. :-( I have nested 2 camera's in a a Empty Game Object... do I need to do this?
I'm a newb.

Thanks for you help.

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 jimm84 · Mar 02, 2017 at 07:50 AM 0
Share

The End Script - -

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class CameraSwitch : $$anonymous$$onoBehaviour {

 public Camera camera1;
 public Camera camera2;

 private void Awake ()
 {
     camera2.enabled = false;
 }

 public void SwitchCam ()
 {
     camera1.enabled = !camera1.enabled;
     camera2.enabled = !camera2.enabled;
 }
     


 void Update()
 {
     if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.C))
     {
         Debug.Log ("C key was pressed");
         SwitchCam ();
     }
 }

}

3 Replies

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

Answer by UnityCoach · Feb 26, 2017 at 01:08 PM

You can group your cameras, but don't have to.

Regarding the switch, you want to make sure you activate one and not the other. Something in the like :

 public Camera camOne;
 public Camera camTwo;
 
 private void Awake ()
 {
     camTwo.enabled = false;
 }
 
 public void SwitchCam ()
 {
     camOne.enabled = !camOne.enabled;
     camTwo.enabled = !camTwo.enabled;
 }

This is one of many ways to do it.

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 jimm84 · Feb 26, 2017 at 04:44 PM 0
Share

Thanks UnityCoach. How would I go about making this work on a keybaord press? For example if I pressed "C" ?

if (Input.Get$$anonymous$$eyDown("C"))

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class CameraSwitch : $$anonymous$$onoBehaviour {

 public Camera camera1;
 public Camera camera2;

 private void Awake ()
 {
     camera2.enabled = false;
 }

 public void SwitchCam ()
 {
     camera1.enabled = !camera1.enabled;
     camera2.enabled = !camera2.enabled;
 }

}

avatar image UnityCoach jimm84 · Feb 26, 2017 at 10:58 PM 0
Share

Et voilà !

 void Update ()
 {
     if (Input.Get$$anonymous$$eyDown("C"))
         SwitchCam ();
 }
avatar image jimm84 UnityCoach · Feb 27, 2017 at 10:34 PM 0
Share

Thanks UC... although it has spat back this error...

"ArgumentException: Input $$anonymous$$ey named: C is unknown UnityEngine.Input.Get$$anonymous$$eyDown (System.String name) (at /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/InputBindings.gen.cs:425) CameraSwitch.Update () (at Assets/CameraSwitch.cs:24)"

Is there something that I have failed to add in the editor?

Thanks!

Show more comments
avatar image
0

Answer by jimm84 · Feb 28, 2017 at 07:40 AM

Morning... Managed to stop it spitting back errors but the camera transitions doesn't work. Another thing to note is that the object in my scene is flash/blinking rapidly. Any clues?

using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class CameraSwitch : MonoBehaviour {

 public Camera camera1;
 public Camera camera2;

 private void Awake ()
 {
     camera2.enabled = false;
 }

 public void SwitchCam ()
 {
     camera1.enabled = !camera1.enabled;
     camera2.enabled = !camera2.enabled;
 }
     


     void Update() {
     if (Input.GetKeyDown(KeyCode.C))
             print("C key was pressed");
         SwitchCam ();

     }

}

Thanks,

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 UnityCoach · Feb 28, 2017 at 01:00 PM 0
Share

Ah, if you want to add more than one line of code to an if statement, you need opening and closing brackets.

 void Update()
 {
      if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.C))
     {
         Debug.Log ("C key was pressed");
         SwitchCam ();
     }
 }

avatar image jimm84 · Mar 02, 2017 at 07:52 AM 0
Share

Hello Unity Coach, thank you for your help! I have also tested this on my mac and it works. I might add some comments to this at some stage. Cheers Thank you!

avatar image
0

Answer by Vampyr_Engel · Apr 15, 2018 at 05:12 AM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Manycameras : MonoBehaviour {
 
     public Camera camera1;
     public Camera camera2;
     public Keycode key;
 
     private void Awake ()
     {
         camera2.enabled = false;
     }
 
     public void SwitchCam ()
     {
         camera1.enabled = !camera1.enabled;
         camera2.enabled = !camera2.enabled;
     }
 }
 
 void Update()
 {
     if (Input.GetKeyDown (KeyCode.C))
     {
         Debug.Log ("C key was pressed");
         SwitchCam ();
     }
 }

Well I tried this code as I wish for more than one camera in my game I want lots as I want to be able to see from both friendlies and enemies but all I keep getting when I run this code is this

Blockquote

: Error CS0116: A namespace cannot directly contain members such as fields or methods (CS0116) (Assembly-CSharp)
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

323 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 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 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 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 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 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 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 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

Top Down Camera View Problem 1 Answer

What this error? 0 Answers

"Lookat" only working in one case 0 Answers

My boolean value won't get called from other script 0 Answers

Rotation around player. and Camera position, 2 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