Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

GetButtonDown / Up is crazy [SOLVED]

Discussion in 'Scripting' started by ossetegames, Jun 26, 2018.

  1. ossetegames

    ossetegames

    Joined:
    Mar 25, 2018
    Posts:
    13
    Hi folks!

    I've been working on this project for a while. Inputs were working just perfectly, like this:

    Code (CSharp):
    1.  
    2. public class PlayerControllerHuman : PlayerController
    3. {
    4.     private string key_left;
    5.     //Added for testing
    6.     private int counter;
    7.  
    8.     private void Start()
    9.     {
    10.         counter = 0;
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         if (Input.GetKeyDown(key_left))
    16.         {
    17.             //Rotate L
    18.             Debug.Log("TESTING button DOWN left----"+counter);
    19.         }
    20.  
    21.         if (Input.GetKeyUP(key_left))
    22.         {
    23.             //Stop Rotating left
    24.             Debug.Log("TESTING button UP left----"+counter);
    25.         }
    26.     }
    27. }
    28.  
    This code used to do the following: the frame when I pressed the button it triggered something (only that frame) and when it was released (UP) it triggered something else.

    Now I just updated to 2018.1.6f1 from 2018.1.3f1 and when I press the key it triggers both down and up EVERY frame when I keep the key pressed. I added the Debug to see the frame count and this is the result:


    TESTING button DOWN left----358
    TESTING button UP left------358
    TESTING button DOWN left----359
    TESTING button UP left------359
    TESTING button DOWN left----360
    TESTING button UP left------360
    TESTING button DOWN left----361
    TESTING button UP left------361
    TESTING button DOWN left----362
    TESTING button UP left------362
    TESTING button DOWN left----363
    TESTING button UP left------363
    TESTING button DOWN left----364
    TESTING button UP left------364
    TESTING button DOWN left----365
    TESTING button UP left------365
    TESTING button DOWN left----366
    TESTING button UP left------366
    TESTING button DOWN left----367
    TESTING button UP left------367


    Any suggestions? Something I forgot to configure?
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Can you try using a KeyCode instead of a string for the button? Might not solve things, but I would recommend using that enum anyway.
     
    ossetegames likes this.
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    not sure if thats just a typo bringing it over to the forums, but there isn't a GetKeyUP, only a GetKeyUp
     
    ossetegames likes this.
  4. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    I tried your code (sort of; see below) in 2017.3.1f1 and it worked fine. I imported the project into 2018.1.6f1 and it also worked fine. Here's the actual code I ran:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class KeyDown : MonoBehaviour
    4. {
    5.     private string key_left = "space";
    6.     //Added for testing
    7.     private int counter;
    8.  
    9.     private void Start()
    10.     {
    11.         counter = 0;
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         if (Input.GetKeyDown(key_left))
    17.         {
    18.             //Rotate L
    19.             Debug.Log("TESTING button DOWN left----" + counter);
    20.         }
    21.  
    22.         if (Input.GetKeyUp(key_left))
    23.         {
    24.             //Stop Rotating left
    25.             Debug.Log("TESTING button UP left----" + counter);
    26.         }
    27.     }
    28. }
    Now, it seems to me that the code you posted probably isn't quite the code you are running. As LeftyRighty noted, there is no "GetKeyUP" method. Also, your posted code lacks any
    using
    statements. Nothing ever sets the
    key_left
    string, and there is no code that increments
    counter
    , so, when I run it, it always appears as zero.

    The class your code creates,
    PlayerControllerHuman
    is a subclass of
    PlayerController
    , which has me wondering what might be happening in superclass code (I am assuming that
    PlayerController
    is a subclass of
    MonoBehaviour
    , is that right?).

    What I did in my test was create an empty
    GameObject
    in my scene and attach the script above to it, then run. I'd suggest you try that and see if the problem goes away. If it does, try making my
    KeyDown
    class a subclass of your
    PlayerController
    class and see if the problem returns. If it does, the problem may be somewhere in that parent class.
     
    ossetegames likes this.
  5. ossetegames

    ossetegames

    Joined:
    Mar 25, 2018
    Posts:
    13
    Hi all! Thank you for taking the time.

    My notes:
    *Yeah, I just copied and pasted the important sections (class name, method where the issue is happening, etc.)
    *
    PlayerController 
    is indeed a subclass of MonoBhaviour.
    *I also forgot to paste here the
    counter++;
    on Update() -my bad :( but it is there. Otherwise the log could never get the results I also pasted on the post.
    *The keys are set when the level is created and it all is linked to a more complex custom controllers system so that's why it's never set.

    What is completely strange for me is that I'm 2000% sure that I had GetButtonDown/Up as mentioned on the header (the UP* was a typo while assembling the post, too).
    key_left 
    has assigned the name of the input that is mapped on the Input Manager. So I'll go back and check the source code from different versions to confirm that In which case I would be absolutely embarrassed.

    Meanwhile, if somebody wants to give it a try I would be totally happy because I'm 9 hours away of getting back home.

    Thanks!
     
  6. ossetegames

    ossetegames

    Joined:
    Mar 25, 2018
    Posts:
    13
    Well, thanks to the Unity cloud service I accessed the code. I will copy and paste it as it truly is to avoid more typos:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class PlayerControllerHuman : PlayerController
    5. {
    6.  
    7.     //Button names for this player
    8.     private string key_left;
    9.     //...
    10.     private int counter;
    11.  
    12.     private void Start()
    13.     {
    14.         //Only for this test folks!
    15.         counter = 0;
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         counter++;
    21.  
    22.         // The reason why I had GetKeyDown/Up in the code
    23.         // I originally posted is because i was using it long time ago as a
    24.         //  shortcut to activate some stuff and I replaced the
    25.         // KeyCode with the buttonName thinking it was the same...duh.
    26.         // I was late for work when posting it, sorry!!
    27.         //test key
    28.         if (Input.GetKeyDown(KeyCode.Keypad9))
    29.         {
    30.             // ...
    31.         }
    32.  
    33.         // This is where the actual issue happens
    34.         if (Input.GetButtonDown(key_left))
    35.         {
    36.             Debug.Log("TESTING button DOWN left----"+counter);
    37.         }
    38.  
    39.         if (Input.GetButtonUp(key_left))
    40.         {
    41.             Debug.Log("TESTING button UP left------"+counter);
    42.         }
    43.     }
    44. }
    Something more I want to mention:
    I had an older version of the code (maybe from a week ago) on my laptop which still has 2018.1.3 so just to prove that it wasn't something I added, I tested that code on my desktop (2018.1.6) and on the laptop simultaneously. It worked on the laptop but had the crazy input issue on the desktop.
     
    Last edited: Jun 26, 2018
  7. ossetegames

    ossetegames

    Joined:
    Mar 25, 2018
    Posts:
    13
    Hi! I know what you mean. In my case, it's part of the controls customization layer I added. See the updated code where I made sure the real code is the one posted (using GetButtonDown/up, so the string makes sense now lol)

    Thank you!
     
  8. ossetegames

    ossetegames

    Joined:
    Mar 25, 2018
    Posts:
    13
    It was a typo and the wrong section of the code posted :eek: Check out the last code posted.

    Thank you!
     
  9. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    I think we'll need the code for the
    PlayerController
    class as well, to help you.
     
    ossetegames likes this.
  10. ossetegames

    ossetegames

    Joined:
    Mar 25, 2018
    Posts:
    13
    I can post it but nothing really happens there. I have two derived classes from PlayerController (PC). One is the Human version and the other is the AI version. Their Update implementation (where all the input is checked) is completely different (AI of course don't have GetButton~) so the Update from PC is never called. Besides that, none of the methods invoked from PC are related to input. I have some overwritten stuff and specific implementation for each derived class but again, nothing about input.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerController : MonoBehaviour
    4. {
    5.     void Update()
    6.     {
    7.         Debug.Log("In the PLAYERCONTROLLER Update(). THIS SHOULD NOT BE EXECUTED!");
    8.     }
    9. }
    Thank you!
     
  11. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,195
    Since it's GetButton calls instead of GetKey calls, it might be that you have some strange settings in your InputManager.
     
    ossetegames likes this.
  12. ossetegames

    ossetegames

    Joined:
    Mar 25, 2018
    Posts:
    13
    Hi! That was my conclusion too, that a new flag or an old one that was usually checked by default is now unchecked by default but as far as I know, all the input manager values are carried with the project (like all the keys I set). I haven't changed anything there.

    Thanks!
     
  13. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    Have you tried attaching my script to an empty
    GameObject
    and running it?
     
    ossetegames likes this.
  14. ossetegames

    ossetegames

    Joined:
    Mar 25, 2018
    Posts:
    13
    I will when I get home, didn't have the chance this morning (to test the GetButton~ alone). I'll get back to you with the results! Please keep an eye on this post :)

    Something else I tried:

    I compared 2 versions of the project--the one I left on my laptop using 2018.1.3 and the most recent on my desktop when I first noticed the issue--using winMerge. None of the changes impacted the input section of the code. That has been working perfectly for the last 6 months :(

    That moment when you start to think that the code was actually working by a miracle.
     
    Last edited: Jun 26, 2018
  15. ossetegames

    ossetegames

    Joined:
    Mar 25, 2018
    Posts:
    13
    Tested, and failed. I created a new project, created an empty GO and a new script. Just changed a little things in your code (like the name and the variable name) so it's easy for others to replicate. I also used a predefined virtual button named "Vertical" (up/down arrows), but it is your code. I still get the same log. Here you have it. Please try it and let me know what happens when you leave the key pressed.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerController : MonoBehaviour
    4. {
    5.     // Using one of the "buttons" already set by default (up:positive/down negative)
    6.     private string key_name = "Vertical";
    7.     private int counter;
    8.  
    9.     private void Start()
    10.     {
    11.         counter = 0;
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         counter++;
    17.         if (Input.GetButtonDown(key_name))
    18.         {
    19.             //Rotate L
    20.             Debug.Log("TESTING button DOWN left----" + counter);
    21.         }
    22.  
    23.         if (Input.GetButtonUp(key_name))
    24.         {
    25.             //Stop Rotating left
    26.             Debug.Log("TESTING button UP left----" + counter);
    27.         }
    28.     }
    29. }
     
  16. ossetegames

    ossetegames

    Joined:
    Mar 25, 2018
    Posts:
    13
    I tested a couple of more things:

    1) I ran the 2018.1.6 code on my old laptop with 2018.1.3 and it works. The log is printed only the frame the key is pressed/released as it should.

    Does this mean it's the unity version? Is it a bug?

    2) I removed the GetButtonDown/Up and left only a GetButton which returns true while the key is pressed and false if it is not pressed. Well, it will return true when pressed but from time to time, the method returns FALSE.

    Is it an error on my input? I mean, my keyboard? That would make sense with the laptop test. I don't have another keyboard to try :/
     
  17. ossetegames

    ossetegames

    Joined:
    Mar 25, 2018
    Posts:
    13
    MYSTERY SOLVED!

    Nop, it wasn't unity! It was somehow my keyboard that is a cheap gaming keyboard but has a weird secret function to alter the transmission time. I tested the keyboard on the laptop and got the error only when the key was pressed on the desktop's keyboard but not on the laptop's keyboard.

    I'm so sorry for this. Never happened before! Lesson learned: understand your hardware, read the manual, and transmission time on keyboards is a weird thing.

    Thank you all by your effort!
     
  18. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    No apologies necessary and I'm glad you figured it out.

    If I may, let me suggest that the lesson to be learned here is actually that when code behaves differently on two different machines, one should always consider that it is the hardware that is the cause. Also (and this is a lesson I learned the hard way a long, long time ago) avoid changing code when you move to a new machine until after your existing code is running on the new machine.

    From your description, it sounds like you changed two things at the same time: you changed versions of Unity (effectively the same as changing your code), and you changed computers. This appears to have misled you into thinking that the new behavior you observed was due to the change in Unity version when, in fact, it was due to the change in machines. Whenever possible, only change one thing at a time.

    Well done on solving your problem!
     
    ossetegames likes this.