Search Unity

Input Questions Suggestions

Discussion in 'Scripting' started by slippyd, Jun 21, 2005.

  1. slippyd

    slippyd

    Joined:
    Jun 11, 2005
    Posts:
    129
    First, a few questions that may lead to feature suggestions. What's the difference between Input.GetButton and Input.GetButtonDown? Same with Input.GetKey Input.GetKeyDown and Input.GetMouseButton Input.GetMouseButtonDown? To me, "Returns whether the button identified by buttonName is pressed." and "Returns whether the button identified by axisName was pressed down this frame." sound the same and seem to do the same thing in a scene.

    Also, are the functions GetInputAxis() and GetInputButton() gone now (renamed)? These appear in the included documentation and the wiki at http://www.otee.dk/tikiwiki/tiki-index.php?page_ref_id=30 .

    Here comes the feature idea: It would be nice if there was a way to get a response back based on if and how many times a key/button/axis was pressed, not its current state. Example when you would need this: in a tetris game, you want the key to repeat at a normal pace to get the piece all the way left or right. However, if the user quickly presses the key 5 times, you want the piece to move 5 times, even if they pressed it faster than your key repeat.

    I have achieved this effect with javascript, but it would be nice if it was built it. What I did was keep track of the last update and the repeat delay and return if it hasn't been long enough since the last update:

    Code (csharp):
    1.     if (Time.time < lastUpdate + repeatDelay) { return; }
    Then, when checking for input, I update the last update var:

    Code (csharp):
    1.     if (Input.GetAxis("Horiz") == 1) {
    2.         transform.position.x += 8;
    3.         lastUpdate = Time.time;
    4.     }
    Then, to get it so quick key pressing still works, I add this as the very first item in the update function to reset the delay when no keys are pressed:

    Code (csharp):
    1. if (Input.GetAxis("Horiz") == 0  Input.GetAxis("Vert") == 0) { lastUpdate = -repeatDelay; }
    However, simple things like this that are run very often and that many people will want to use are probably better to put in the engine, because that way they run faster. Just an idea.
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    GetButtonDown and GetButton:

    GetButton returns true while the user is holding down the button
    GetButtonDown returns true the first frame the user is pressing the button.

    GetInputAxis/GetInputButton has been renamed to GetAxis/GetButton.


    I think that by using the GetButtonDown function you can simplify your code a lot already.
     
  3. slippyd

    slippyd

    Joined:
    Jun 11, 2005
    Posts:
    129
    Weird... now GetButtonDown works. Still, it will require scripting code to get a button to fire once and to repeat, like a letter key in a text editor.
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    If you want to get the same behaviour as normal text input you should use Input.inputString.

    It will contain what the user has typed this frame.