Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by brentshermana · Jun 21, 2015 at 06:05 PM · keyboarduser interfacekeyboard input

OnGUI() keyboard input

I don't have a very good understanding of OnGUI() or getting player input, and so the following code that I use technically works, but isn't very pleasant to use.

without further ado, here's the code:

 void OnGUI() {
         Event e = Event.current;
         if (e.isKey) {
             //debug:
             Debug.Log ("key read: " + e.keyCode);
             
             if ((e.keyCode == KeyCode.Keypad1 || e.keyCode == KeyCode.Alpha1) && (moveOptions.Count > 0)) {
                 move(moveOptions[0]);
             } else if ((e.keyCode == KeyCode.Keypad2 || e.keyCode == KeyCode.Alpha2) && (moveOptions.Count > 1)) {
                 move(moveOptions[1]);
             } else if ((e.keyCode == KeyCode.Keypad3 || e.keyCode == KeyCode.Alpha3) && (moveOptions.Count > 2)) {
                 move(moveOptions[2]);
             } else if ((e.keyCode == KeyCode.Keypad4 || e.keyCode == KeyCode.Alpha4) && (moveOptions.Count > 3)) {
                 move(moveOptions[3]);
             } else if ((e.keyCode == KeyCode.Keypad5 || e.keyCode == KeyCode.Alpha5) && (moveOptions.Count > 4)) {
                 move(moveOptions[4]);
             } else if ((e.keyCode == KeyCode.Keypad6 || e.keyCode == KeyCode.Alpha6) && (moveOptions.Count > 5)) {
                 move(moveOptions[5]);
             } else if ((e.keyCode == KeyCode.Keypad7 || e.keyCode == KeyCode.Alpha7) && (moveOptions.Count > 6)) {
                 move(moveOptions[6]);
             } else if ((e.keyCode == KeyCode.Keypad8 || e.keyCode == KeyCode.Alpha8) && (moveOptions.Count > 7)) {
                 move(moveOptions[7]);
             } else if ((e.keyCode == KeyCode.Keypad9 || e.keyCode == KeyCode.Alpha9) && (moveOptions.Count > 8)) {
                 move(moveOptions[8]);
             }
         }
     }

Now here's the problem: let's say I'm in a situation where I'm given the option to move to a specific location using '1'. If I do that, I am moved there and then to wherever '1' takes me from that location, and so on, for as long as I hold down '1'. This becomes very inconvenient because even if I quickly tap and release the button, Unity will read that as two moves.

How do I alter my code so that I only get one move per 'tap' (press/depress) of the keyboard?

UPDATE: READ... I was actually able to fix this on my own, using this code instead:

 void Update () {
         if ((Input.GetKeyDown ( KeyCode.Keypad1) || Input.GetKeyDown (KeyCode.Alpha1)) && (moveOptions.Count > 0)) {
             move(moveOptions[0]);
         } else if ((Input.GetKeyDown(KeyCode.Keypad2) || Input.GetKeyDown(KeyCode.Alpha2)) && (moveOptions.Count > 1)) {
             move(moveOptions[1]);
         } else if ((Input.GetKeyDown(KeyCode.Keypad3) || Input.GetKeyDown(KeyCode.Alpha3)) && (moveOptions.Count > 2)) {
             move(moveOptions[2]);
         } else if ((Input.GetKeyDown(KeyCode.Keypad4) || Input.GetKeyDown(KeyCode.Alpha4)) && (moveOptions.Count > 3)) {
             move(moveOptions[3]);
         } else if ((Input.GetKeyDown(KeyCode.Keypad5) || Input.GetKeyDown(KeyCode.Alpha5)) && (moveOptions.Count > 4)) {
             move(moveOptions[4]);
         } else if ((Input.GetKeyDown(KeyCode.Keypad6) || Input.GetKeyDown(KeyCode.Alpha6)) && (moveOptions.Count > 5)) {
             move(moveOptions[5]);
         } else if ((Input.GetKeyDown(KeyCode.Keypad7) || Input.GetKeyDown(KeyCode.Alpha7)) && (moveOptions.Count > 6)) {
             move(moveOptions[6]);
         } else if ((Input.GetKeyDown(KeyCode.Keypad8) || Input.GetKeyDown(KeyCode.Alpha8)) && (moveOptions.Count > 7)) {
             move(moveOptions[7]);
         } else if ((Input.GetKeyDown(KeyCode.Keypad9) || Input.GetKeyDown(KeyCode.Alpha9)) && (moveOptions.Count > 8)) {
             move(moveOptions[8]);
         }
     }

However, I'd still like an explanation of why each of these code snippets works in the way that they do;

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 Flightkick · Jun 21, 2015 at 06:29 PM

OnGUI() is for GUI stuff, I think it's still from the legacy GUI system. For game mechanics you should use the Update() method. I have never tried OnGUI() using the Event for button press checks.

Might be interesting to take a look at Input.GetButtonDown() where you can specify keys declared in the inputmanager. This way a player can change the keybindings in the launcher.

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 Chom1czek · Oct 04, 2015 at 02:38 PM 0
Share

Hello I would like to ask why shouldn't we use OnGUI() for checking the input? Is it wrong for some reason or just because thats how it is? I'm an inexperienced Unity user so that common knowledge would help me a lot. I'm asking cause Update is calling every frame am I right? And events happen everytime and if you add your event listener then you will catch the event everytime but in Update there is still possibility to not get it, highly unlikely but what if framerate drops for any reason? Will input in update still manage to catch it? And still it sounds more appropriate to catch an event when it happens than checking it every update if it occured isn't it?

Cheers :)

avatar image tanoshimi Chom1czek · Oct 04, 2015 at 02:44 PM 0
Share

Update() happens every frame. Therefore you can't miss user input if you check it there. OnGUI() happens more than once a frame and as @Flightkick points out, it's the legacy pre-4.5 GUI system. It was never designed for user input and, tbh, shouldn't really be used at all now.

avatar image Chom1czek tanoshimi · Oct 04, 2015 at 03:19 PM 0
Share

What if my input happens between frames? And if OnGUI() happens more than once a frame it's more accurate doesn't it? I'm just curious, I'm not trying to be rude :) I just want to know what about performance and accuracy :) Thanks for answer!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to change the editor's keyboard to AZERTY layout? 1 Answer

how make to make ios keyboard's return key submit input? 0 Answers

How do you make a method that gets called when a button gets highlighted using the arrow keys? 0 Answers

Google Featuring Requirement on Android Themes 1 Answer

MonoDevelop: command a sometimes all, sometimes replace 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