Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Orloffyeah · Jan 19, 2013 at 02:05 AM · camerabuttonchange

Changing Cameras With One Button

Hi, I have setup in my game a script which changes the cameras with different buttons, but I want to make it so that I can use the same button to change between cameras, instead of using 2 buttons. How could I solve this problem?

Current Camera Script

 public var rearCamera : Camera ;
 public var driverCamera : Camera ;
 
 
 function Start() {
         rearCamera.camera.enabled = true;
         driverCamera.camera.enabled = false;
 }
 
 function Update() {
 
 if (Input.GetKey(KeyCode.I))
     {
         rearCamera.camera.enabled = false;
         driverCamera.camera.enabled = true;
     }
     
 else if (Input.GetKey(KeyCode.O))
     {
         driverCamera.camera.enabled = false;
         rearCamera.camera.enabled = true;
     }
 }
Comment
Add comment
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

4 Replies

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

Answer by justin35f · Jan 19, 2013 at 04:01 AM

I just ran this and it works pretty well I think.

 #pragma strict
 
 public var rearCamera : Camera ;
 public var driverCamera : Camera ;
 
 
 function Start() {
     rearCamera.camera.enabled = true;
     driverCamera.camera.enabled = false;
 }
 
 function Update() {
     if(Input.GetButtonDown("CameraSwitch"))
     {
         rearCamera.camera.enabled = !rearCamera.camera.enabled;
         driverCamera.camera.enabled = !driverCamera.camera.enabled;
     }
 }

I used Input.GetButtonDown, because if you use GetKey it will detect a button press every from that the button is down. So unless you have lightning fingers, or want to swap camera's every frame, this isn't ideal. You will need to go to Edit/Project Settings/Input then expand 'Axes'. Increase size by one. This will copy the last axes. Rename it, and set the 'Positive Button' to the button you want to be pressed. In the GetButtonDown function, pass in whatever you named the axes to.

If you need further help, or if this is not the solution you are looking for, let me know.

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 Orloffyeah · Jan 19, 2013 at 04:41 AM 0
Share

Thanks, it is working perfectly :D, but could you help me adding another camera? This one should be only active while a button is pressed, otherwise it returns to the previous camera. Something like this :

if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.O)) { reverseCamera.camera.enabled = true; rearCamera.camera.enabled = false; driverCamera.camera.enabled = false; }

avatar image justin35f · Jan 19, 2013 at 05:14 AM 0
Share

No problem. It's actually pretty simple. Add this in your variable declarations:

public var reverseCamera : Camera;

Now add this into the Update function.

if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.P)) { reverseCamera.camera.enabled = true; } else { reverseCamera.camera.enabled = false; }

avatar image Orloffyeah · Jan 19, 2013 at 04:21 PM 0
Share

Thanks it is working :D!

avatar image Mrhhdestroyer · May 18, 2021 at 05:05 PM 0
Share

My Errors:

Assets\Scripts\Gun-Shooting\PlayerWeapon.cs(15,27): error CS1003: Syntax error, ',' expected

Assets\Scripts\Gun-Shooting\PlayerWeapon.cs(15,29): error CS1002: ; expected

Assets\Scripts\Gun-Shooting\PlayerWeapon.cs(15,35): error CS1519: Invalid token ';' in class, struct, or interface member declaration

Assets\Scripts\Gun-Shooting\PlayerWeapon.cs(16,26): error CS1003: Syntax error, ',' expected

Assets\Scripts\Gun-Shooting\PlayerWeapon.cs(16,28): error CS1002: ; expected

Assets\Scripts\Gun-Shooting\PlayerWeapon.cs(16,34): error CS1519: Invalid token ';' in class, struct, or interface member declaration

My Script: public class PlayerWeapon : MonoBehaviour {

      public GameObject bulletPrefab;
  
      public Transform bulletSpawn;
  
      public float bulletSpeed = 30;
  
      public float lifeTime = 3;
  
      public var mainCamera : Camera;
      public var gunCamera : Camera;
      
      void Start()
      {
          mainCamera.camera.enabled = true;
          gunCamera.camera.enabled = false;
      }
      
      void Update()
      {
          if(Input.GetKey(KeyCode.Mouse1))
       {
          mainCamera.camera.enabled = !mainCamera.camera.enabled;
          gunCamera.camera.enabled = !gunCamera.camera.enabled;
       }
  
          if (Input.GetMouseButtonDown(0))
          {
              Fire();
          }
  
          if (Input.GetKey(KeyCode.Mouse2))
          {
              Fire();
          }
      }
avatar image Orloffyeah Mrhhdestroyer · May 18, 2021 at 05:23 PM 0
Share

Hi, before answering, please be nice when asking for help, saying something like "I've tried this code but is not working, can you please help me?". Just posting a wall of errors and your code hoping someone will answer is rude and this site's community is built on good manners and etiquette.

Anyhow, I think your problem is derived from a language confusion, since when I asked this question a long time ago, it was programmed in UnityScript, while you are running C#. In C#, the var keyword can only be used inside methods, not to declare class variables. For example:

 void MyMethod()
 {
     var tempVar = 3;
 }

To fix your error, you have to specify what type of variable the cameras are, which in the long run is a good practice as it makes code much easier to understand and helps avoid undesired errors. Your variables should be like this:

 public Camera mainCamera;
 public Camera gunCamera;

Hope this helps you and allows you to move forward with your game. Good luck!

avatar image
0

Answer by eXtremeTY · Jan 19, 2013 at 02:58 AM

Not tested, but should work.. ;)

 public var rearCamera : Camera ;
 public var driverCamera : Camera ;
 public toggle : boolean = false;
 
 function Start() {
         rearCamera.camera.enabled = true;
         driverCamera.camera.enabled = false;
 
 }
 
 function Update() {
 
 if (Input.GetKey(KeyCode.I) && !toggle)  //<---- enable rear camera
     {
         rearCamera.camera.enabled = true;
         driverCamera.camera.enabled = false;
         toggle = true;
     }
 
 else if (Input.GetKey(KeyCode.I) && toggle)  //<---- enable driver camera
     {
         rearCamera.camera.enabled = false;
         driverCamera.camera.enabled = driver;
         toggle = false;
     }
 }
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 Orloffyeah · Jan 19, 2013 at 04:02 AM 0
Share

Hi, thanks for your answer, but it has a little "bug", sometimes when I press the button it changes fo a milisecond to the other camera and then returns to the other, do you know how to fix it?

avatar image eXtremeTY · Jan 20, 2013 at 05:52 PM 0
Share

I just seen it now, sorry, was away.. I'll just leave it as it is seeing as someone else helped you out. :) Good luck!

avatar image
0

Answer by iahr · Jul 26, 2016 at 08:06 AM

Try this code. I make this code switching to 2 cameras. using UnityEngine; using System.Collections; public class CameraManagerScr : MonoBehaviour { %|1051292963_1|% %|446312010_2|% %|794809699_3|% %|1039055902_4|% %|-552888617_5|% %|977180863_6|% %|-934038197_7|% %|1782251902_8|% %|-1412472038_9|% %|-813138912_10|% %|-21545877_11|% %|-207334728_12|% %|837798027_13|% %|-804862433_14|% %|270663048_15|% %|-1892770785_16|% %|-1459013006_17|% %|1742211472_18|% %|-1850636563_19|% %|-1519131145_20|% %|1347906305_21|% %|901506400_22|% }

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 zenforhire · May 18, 2021 at 06:34 PM

Your case only works because there is ONLY one object interested in the toggle. What happens when there are 5 objects interested in the same toggle?

We created a Toggle class that sends events when toggled. Objects interested in it toggling listen for the event. The button OnClick calls the toggle method on the Toggle class. The button does NOT need to know who is interested in the change.

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

14 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

Related Questions

Move camera upon button press 0 Answers

How to display UI elements in front of the gameObject? 1 Answer

Change Text of GUI Button from Script 2 Answers

Limiting the amount of times a button can be pressed? 1 Answer

How do I attach a GameObject when the script is attached to unity's on click () function (for buttons). 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