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

Holding the keyboard button down lowers fps

Discussion in 'Editor & General Support' started by Enixoid_v2, Feb 12, 2021.

  1. Enixoid_v2

    Enixoid_v2

    Joined:
    Mar 11, 2014
    Posts:
    49
    OS:
    - windows 8.1 embedded
    - ubuntu 18.04

    Unity version:
    - 2020.15
    - 2020.23

    Hello. Unity has a performance issue. If I press and hold any keyboard button, fps will be halved. It doesn't matter which project I use. I can create an empty project, open an empty scene, hold down any keyboard button - fps will drop almost twice. Has anyone else encountered this problem?

    The problem exists in the editor and in the release build

    Buttons like CAPS LOCK or CTRL do not lower fps.

    The solution from the link doesn't work for me, but the problem is similar: https://answers.unity.com/questions/186343/why-does-holding-down-any-button-cause-20-30-fps-l.html

    There is no such problem in godot 3.2.3 :)
     
    Last edited: Feb 13, 2021
  2. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    451
    This is not normal and there's something wrong with your Input code. Sounds like you are doing something in your code every frame if a key is depressed.
    Input.GetKeyDown
    returns
    true
    only in the frame when the player first depresses the key, and you should use
    GetKeyDown
    and
    GetKeyUp 
    in some kind of if-statements to capture these events. Make sure you are performing the check from your Update function to be sure to actually capture it.

    https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html
     
  3. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    451
    Here's one way to handle your input, using delegate methods.

    Code (CSharp):
    1. private delegate void InputHandler(KeyCode code, bool down);
    2.  
    3. private Dictionary<KeyCode, InputHandler> KEYS = new Dictionary<KeyCode, InputHandler>();
    4.  
    5. void Start() {
    6.     KEYS.Add(KeyCode.Q, RotationKeyPressed);
    7.     KEYS.Add(KeyCode.E, RotationKeyPressed);
    8.     KEYS.Add(KeyCode.K, GrenadeKeyPressed);
    9. }
    10.  
    11. public void Update() {
    12.     RegisterKeyPress(KEYS);
    13. }
    14.  
    15. private void RegisterKeyPress(Dictionary<KeyCode, InputHandler> keySet) {
    16.     foreach (KeyCode keyCode in keySet.Keys) {
    17.         if (Input.GetKeyDown(keyCode)) {
    18.             keySet[keyCode](keyCode, true);
    19.         }
    20.         if (Input.GetKeyUp(keyCode)) {
    21.             keySet[keyCode](keyCode, false);
    22.         }
    23.     }
    24. }
    25.  
    26. private void RotationKeyPressed(KeyCode code, bool down) {
    27.     if (down)
    28.         // respond to rotation key down
    29.     else
    30.         // respond to rotation key up
    31. }
    32.  
    33. private void GrenadeKeyPressed(KeyCode code, bool down) {
    34.     // etc
    35. }
    36.  
     
  4. Enixoid_v2

    Enixoid_v2

    Joined:
    Mar 11, 2014
    Posts:
    49
  5. Enixoid_v2

    Enixoid_v2

    Joined:
    Mar 11, 2014
    Posts:
    49
    Nope. It didn't help either. I don't know what to do.
     
    Last edited: Feb 13, 2021
  6. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    451
    Then your movement/action code is such that during keypress you do something unnecessarily heavy (approximate pi) every frame
     
  7. Enixoid_v2

    Enixoid_v2

    Joined:
    Mar 11, 2014
    Posts:
    49
    As I said before, an empty scene (without scripting) shows the same result.

    So far, I have configured all controls for the mouse. It will do for now.
     
  8. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    451
    Yeah well, we are out of luck as I can't hint to what could be wrong. I can just verify that for my 3D PC game there is no FPS impact either in editor or the actual game despite movement relies on key hold. This is just using the Input class.

    On Ubuntu you can eg
    xev
    to see if OS keyboard events are firing while you run your editor and game. This shouldn't be the case as your game/editor should have exclusive focus.