Search Unity

Resolved Equivalent of Input.GetButton in new input system

Discussion in 'Input System' started by rhys_vdw, Aug 26, 2021.

  1. rhys_vdw

    rhys_vdw

    Joined:
    Mar 9, 2012
    Posts:
    110
    So I think I've worked out
    GetButtonDown
    and
    GetButtonUp
    .

    Code (CSharp):
    1. PlayerInput _playerInput;
    2. InputAction _jumpAction;
    3. InputAction _fireAction;
    4.  
    5. void Start() {
    6.   var map = _playerInput.currentActionMap;
    7.   _jumpAction = map.FindAction("Jump", true);
    8.   _fireAction = map.FindAction("Fire", true);
    9. }
    10.  
    11. static bool IsDown(InputAction action) => action.phase == InputActionPhase.Performed;
    12. static bool IsUp(InputAction action) => action.phase == InputActionPhase.Canceled;
    13.  
    14. void Update() {
    15.   if (IsDown(_jumpAction)) {
    16.     // Start jump
    17.   } else if (IsUp(_jumpAction)) {
    18.     // Release jump
    19.   }
    20.  
    But how do we check if a button is held this frame, as opposed to just pressed?

    Can I also just say that this API is a real pain to work with. If I'm not missing something events seem like the wrong choice for games where we want to manually poll input every frame, and order of execution is important. We've burned many hours just trying to implement a simple jump feature.

    Also, even to just work out the above I had to do a deep dive into the scripting reference. I'm still not sure if it's correct.
     
  2. scantsuns

    scantsuns

    Joined:
    Jun 23, 2017
    Posts:
    6
    I solved this by using the player input generated On"xyz"() functions. I then set the input type to value. Value allows a floating-point number between 0 and 1 and will be called as long as it is greater than 0. Use a variable to store the InputValue and if it is greater than 1, the button is pressed. Also, if you have access to get button-down and get button-up, can't you just have a bool that is set to false on up and true on down?
     
  3. rhys_vdw

    rhys_vdw

    Joined:
    Mar 9, 2012
    Posts:
    110
    Trying to poll the state to avoid using events, and instead read the value directly. Are you saying that you used the Unity events system, or the new code generation system?
     
  4. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    As of 1.1-pre.6.

    Code (CSharp):
    1. myAction.WasPressedThisFrame();
    2.  
    3. myAction.WasReleasedThisFrame();
    4.  
    5. myAction.IsPressed();
     
    Optimus_L and MattDavis like this.
  5. scantsuns

    scantsuns

    Joined:
    Jun 23, 2017
    Posts:
    6
    there's already a better answer, but yeah I used the unity event system.
     
  6. rhys_vdw

    rhys_vdw

    Joined:
    Mar 9, 2012
    Posts:
    110
    Thanks!
     
  7. rhys_vdw

    rhys_vdw

    Joined:
    Mar 9, 2012
    Posts:
    110
    Um... @Rene-Damm. It seems that the package manager doesn't recognise that version because it doesn't conform to semver.


    Code (CSharp):
    1. An error occurred while resolving packages:
    2.   Project has invalid dependencies:
    3.     com.unity.inputsystem: Version '1.1-pre.6' is invalid. Expected one of: a 'SemVer' compatible value; a value starting with 'file:'; a Git URL starting with 'git:' or 'git+', or ending with '.git'.
    4.  
    5. A re-import of the project may be required to fix the issue or a manual modification of C:/Users/rhysv/Projects/Moball/Packages/manifest.json file.
     
  8. rhys_vdw

    rhys_vdw

    Joined:
    Mar 9, 2012
    Posts:
    110
    Also new release marked on github repo is not available through registry?

    upload_2021-8-30_12-40-1.png
     
  9. rhys_vdw

    rhys_vdw

    Joined:
    Mar 9, 2012
    Posts:
    110
    Furthermore the revision you mentioned is not even tagged in the repo...

    upload_2021-8-30_12-47-41.png
     
  10. rhys_vdw

    rhys_vdw

    Joined:
    Mar 9, 2012
    Posts:
    110
    Okay, managed to update with this line:

    Code (CSharp):
    1.     "com.unity.inputsystem": "git://github.com/Unity-Technologies/InputSystem.git?path=Packages/com.unity.inputsystem#1.1.0",
     
  11. You can update through package manager this way. Just change the 5 to 6. It is working, no need for the git repo if you don't want for something specifically.
     
  12. rhys_vdw

    rhys_vdw

    Joined:
    Mar 9, 2012
    Posts:
    110
    1.1.0 is not available in the registry. Surely I should take stable version over pre.6 now that it's on the repo?

    https://github.com/Unity-Technologies/InputSystem/pull/1398
     
  13. It isn't released yet.

    But I'm a bit confused, in your previous post you tried to pull the
    1.1-pre.6
    version (but the version number you used is wrong) which should be
    1.1.0-pre.6
    . That's why I mentioned that you can install it through the package manager, just follow the link, I wrote it in details, step by step.
     
  14. rhys_vdw

    rhys_vdw

    Joined:
    Mar 9, 2012
    Posts:
    110
    That would likely work. I just used the version number that @Rene-Damm gave me. However that was before the 1.1.0 "release" (although looking at the PR it doesn't have test coverage yet). Either way I've burned enough time on this one. Thanks for the pointer for future. I'm pretty sure that UI interface is just the same as modifying the manifest.json directly in any case.
     
  15. rhys_vdw

    rhys_vdw

    Joined:
    Mar 9, 2012
    Posts:
    110
    @Lurking-Ninja Actually, do you know how to get a list of releases? It seems they're hidden in the package manager even if I have preview packages enabled. All this confusion could have been sidestepped if they were visible.
     
  16. TrueBlue25

    TrueBlue25

    Joined:
    Aug 27, 2021
    Posts:
    7

    Yes, but how to use this in an if statement? Doesnt seem to work for me:p
     
  17. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,476
    so what is answer of original question?

    what is same function code of old functions(GetButtonDown, GetButton, GetButtonUp)?
     
  18. I recommend reading the guide migrating from the Input Manager. You can find it here: GetButton, GetButtonDown and GetButtonUp.
     
    eliteforcevn likes this.