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 JoeVRoh · Jul 13, 2016 at 11:14 PM · camerascripting problemscripting beginnerscriptableobjectcameras

Why are my keys being registered multiple times?

So I've been working on a multiple-camera system. 1 = Main Camera 2 = First Person 3 = Third Person When I press a key, the console is reporting my Log as pressing key(s) (each key I press) 4 times.

I've been able to get this camera switch to work two times (I've had to make this game three times due to losing my computers and sending them in for repairs). For some reason it does not want to cooperate this time.

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour 
 {
     public Camera MainCamera; // Freeform camera (Panable around the character)
     public Camera FirstPerson; // First Person Perspective
     public Camera ThirdPerson; // Third Person Camera (Over the shoulder)
 
     public GameObject player; // Player reference object
 
     private Vector3 offset; // Offset Vector for distance from the player
 
     // Use this for initialization
     void Start () 
     {
         // Camera default settings
         #region
 
         MainCamera = GetComponent<Camera>();
         FirstPerson = GetComponent<Camera>();
         ThirdPerson = GetComponent<Camera>();
 
         MainCamera.enabled = true;
         FirstPerson.enabled = false;
         ThirdPerson.enabled = false;
 
         #endregion
 
         // Offset is equal to the transform position minus the player's transform position
         offset = transform.position - player.transform.position;
     }
     
     // Update is called once per frame
     void Update () 
     {
 
         // Camera Follows the Player
         #region
 
         // Camera will transform its position relative to the player's position and the offset
         transform.position = player.transform.position + offset;
 
         #endregion
 
         // Main Camera (Freelow)
 
         if (Input.GetKey(KeyCode.Alpha1)) // Press of the 1 key
         {
             Debug.Log( "Main Camera Enabled, 1 Key pressed." );
             MainCamera.enabled = MainCamera.enabled;
             FirstPerson.enabled = !FirstPerson.enabled;
             ThirdPerson.enabled = !ThirdPerson.enabled;
         }
 
         // First Person Camera
 
         if (Input.GetKey(KeyCode.Alpha2)) 
         {
             Debug.Log( "First Person Camera Enabled, 2 Key pressed." );
             FirstPerson.enabled = FirstPerson.enabled;
             MainCamera.enabled = !MainCamera.enabled;
             ThirdPerson.enabled = !ThirdPerson.enabled;
         }
 
         // Third Person Camera
 
         if (Input.GetKey(KeyCode.Alpha3))
         {
             Debug.Log( " Third Person Camera Enabled, 3 key pressed." );
             ThirdPerson.enabled = ThirdPerson.enabled; // Enable the Third Person Camera
             MainCamera.enabled = !MainCamera.enabled; // Disable the Main Camera
             FirstPerson.enabled = !FirstPerson.enabled; // Disable the First Person Camera
         }
 
     }
 }

I thought that maybe the regions of code I had these stored in were affecting them but that doesn't seem to be the case. I've tried making these cameras change to simple false instead of !Camera.enabled (toggled) so see if there's a difference and it just makes it so no full screen camera is active.

Thanks in advance. This is driving me nuts! If there's an easier or 'better' way to do this I'd love to know.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JoeVRoh · Jul 13, 2016 at 11:19 PM

By removing the toggles (!Camera.enabled) I've alleviated the cameras switching from each other too fast to see.

My keys are still being pressed 4 times each though. Would it be better to use FixedUpdate or LateUpdate?

Also, what about GetKeyDown or Up or just GetKey? I'm attempting to understand these differences. That's why I find the core issue is a key being processed multiple times within the update.

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

87 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

Related Questions

Switch between 5 cameras in the game by clicking. 0 Answers

How can I get all cameras enabled true false states ? 0 Answers

Open door by completing all puzzles 1 Answer

CAMERAS ERROR , I NEED HELP 0 Answers

Hi Im having a problem with unity and creating a criptable object from a script.,Hi so im going through a course and learning unity, and i've run into a problem 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