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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem with delayed Input.GetKey

Discussion in 'Scripting' started by Nero-R, Sep 14, 2015.

  1. Nero-R

    Nero-R

    Joined:
    Dec 29, 2014
    Posts:
    5
    I am making a grid based game but i run into a big problem, i want to set certain speed to the movement between tiles, for now my character is teleporting (not adding the animations right now beacause i dont have them yet) but my problem is that update triggers the keydown really fast so i cant set the speed i used this code to solve that, but i got a new problem, from time to time a keypress wont trigger, i press the right arrow and half of the times it wont react, it works fine if i hold the key down, any insight on how to solve this?, i would spect some keypress to be ignored after moving, but this code ignores a lot of other keypress with not prior movement whatsoever.

    Code (CSharp):
    1.     public float keyDelay = 0.5f;
    2.     private float timePassed = 0f;
    3.     void Update () {
    4.         timePassed += Time.deltaTime;
    5.         if (timePassed >= keyDelay) {
    6.             timePassed = 0f;
    7.             if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D)) {
    8.                 int newX = mainCharacter.getX () + 1;
    9.                 if (!board [newX, mainCharacter.getY ()].isBlocked ()) {
    10.                     board [newX, mainCharacter.getY ()].addCharacter (mainCharacter);
    11.                     board [mainCharacter.getX (), mainCharacter.getY ()].removeCharacter ();
    12.                     mainCharacter.setCharacterPosition (newX, mainCharacter.getY ());
    13.                 }
    14.             }
    15.         }
    16.  
    17.     }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you need to restructure your code... at the moment it will only detect a keypress in the exact frame where it registers 0.5 seconds have passed.

    Consider using a bool to "lock out" the user input for a while

    Code (csharp):
    1.  
    2. public float keyDelay = 0.5f;
    3. private float timePassed = 0f;
    4. public bool locked;
    5.  
    6. void Update()
    7. {
    8.     if(locked)
    9.     {
    10.         timePassed += Time.deltaTime;
    11.         if(timePassed >= keyDelay)
    12.         {
    13.             timePassed = 0;
    14.             locked != locked;
    15.         }
    16.     }
    17.  
    18. // pseudo from here :P
    19.     if(!locked && Input.Get... blah blah blah)
    20.     {
    21.     locked = true;
    22.     // do stuff
    23.     }
    24. }
    25.  
     
    Nero-R likes this.
  3. Nero-R

    Nero-R

    Joined:
    Dec 29, 2014
    Posts:
    5
    thanks, that works, i wonder if there is a better way to do it tho hahaha i am really new to programing games, i had some working ideas but i did not like the extra code they produce, this one is simple nuf thanks