Hi, I try to make a 2D game using the new input system. But i have a problem : I don't know how to recognize if I'm pressing or releasing a button. I want to make my jump like that : - when I press space, I jump - when I release space, I stop the jump But I don't know how to know if i'm pressing or releasing in the event i call. Can someone give me a solution ? Thank you.
Yes I used that before to know the new Input System, but now i'm not sure this is the right option. Because i'm not using Input.getKeyDown (or up) but event with action map
I am also curious as to how this works in the new Input system. I can get my OnButton callback triggered on both key down and key up, but calling context.ReadValue<bool>() results in a bunch of exceptions. I cant see anything else in the context which would indicate that this is a key down or a key up. Am I supposed to set something special in the Action bindings?
If you look at the Action binding, there should be a "Continuous" flag. With that you can get started phase for down, Performed Phase for held, and Cancel Phase for released. EDIT: For a button action, there's also the PressAndRelease flag on the Press Interaction.
Didn't work quite like that for me. So I went and checked these variables and setting exhaustively, and finally found a combination that works: Set Continuous to false. Set Interactions to Press. This gives you one callback on key down, and one on key up, and you can tell which is which by the value of context.performed. Using continuous doesn't allow you to see which msg is the unique key down, but cancelled was true only once on key up. I feel that this is unnecessarily complicated and not at all intuitive. Maybe Unity could clean it up a bit?
I don't mess with Continuous and Interactions at all. The interactions/continuous situation is like - InputSystem: "You want advanced functionality?", Me: "Yes, sure.", InputSystem: "Great... but that will cost you basic functionality!". Depending on Continuous/Interaction combo, Started and Canceled events may or may not be called at all and Performed may be at the beginning, or at the end, or for the whole duration. IMO Started, Performed, Canceled should be events that are 100% guaranteed/stable functionality. And there should be pollable version of them (in specific situations polling an input is all you need instead of constantly subbing/unsubbing for events, or enabling/disabling). BTW next is what I use for polling InputAction/ButtonControl. Haven't tested it enough but it seems to work so far. Code (CSharp): public bool GetButtonDown(InputAction inputAction) // public bool GetButton(InputAction inputAction) // public bool GetButtonUp(InputAction inputAction) { if (inputAction.lastTriggerControl is ButtonControl bc) { return bc.wasPressedThisFrame; // return bc.isPressed; // return bc.wasReleasedThisFrame; } return false; // warning? }
Hi guys, I faced this problem no later than yesterday and I think i'm using a much shorter and cleaner solution. Without any modifier on the InputAction, I just let it to the type "Button". Then you just have to register on the "started" and on the "cancelled" events. Hope that helps anybody that pass by. Code (CSharp): GameInput myActions = new GameInput(); myActions.Main.Shoot.started += _ => {Debug.Log("Started pressing space");}; myActions.Main.Shoot.canceled += _ => {Debug.Log("Released space");};
I've managed to solve this by using the returning value of the action Jump to check when is pressed and when is released. While it's pressed it returns 1, if it's not then it returns 0. Code (CSharp): //Call this function on FixedUpdate() void modifyPhysics() { // Set returning value of the action Jump to a variable float jumpState = (controls.Player.Jump.ReadValue<float>()); if (jumpState == 0 && !isGrounded) { rb.gravityScale = gravity * (fallMultiplier / 2); } }
Note that as of 1.1-preview.2, there were some extensions to the InputAction API to work with buttons more easily. (CHANGELOG, docs). Code (CSharp): // GetButton("fire") playerInput.actions["fire"].IsPressed() // GetButtonDown("fire") playerInput.actions["fire"].WasPressedThisFrame() // GetButtonUp("fire") playerInput.actions["fire"].WasReleasedThisFrame()
Version 1.1-preview.2 is not available in the package Manager, at least on Unity 2019.4.13f1. I was looking for a solution to the same problem and had to invent a Bicycle to solve it. At the moment, "new input system" is not convenient for development.
Please tell me there's a more direct event for a key being released now and this isn't still the "official" solution to this problem?
Why don't you read it for yourself? https://docs.unity3d.com/Packages/c...ocsunity3dcomscriptreferenceinputgetkeyuphtml
That doesn't help much since it's not using InputAction. At that point might as well use the old system. I'm curious about what the best practice is too (using Actions). I tried polling .triggered in Update() (directly replacing the old GetKey/GetButton) but it works completely inconsistently in my project. And side note as I'm typing 1.0.2 is the latest release version and (in my 2021.1.15f project enabling pre-release packages doesn't make them show up no matter the amount of refreshes or restarts).
For now I've settled on doing something like: Code (CSharp): _jumpInput.performed += _ => OnJump(true); _jumpInput.canceled += _ => OnJump(false); _jumpInput is an InputAction. Seems to work as intended so far. Still curious about the proper way to do it with polling.
You said You haven't mentioned any actions... What does this even mean? What do you mean "it works completely inconsistently"? You really shouldn't poll, but if you want, .triggered is the right way. Because of this you only can install unreleased versions this way. This isn't quite polling though and this is a proper way of handling things.