Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to see if PlayerInput action was performed? Unity, New Input System

I’m using the new inptu system, and i want a way to see if an input action has been performed (only once, like GetButtonDown).
If I use ".ReadValue()", the if statement returns true not only once, but for as long as the "inventoryAction button" is held down.

    private void Awake()
    {
        playerInput = GetComponent<PlayerInput>();
        inventoryAction = playerInput.actions["Inventory"];
    }
    private void Update()
    {
         if (inventoryAction.ReadValue<float>() == 1)
         {
            Debug.Log("inventory key was pressed");
         }
    
    }

How do I only get the "performed" part in an if statement (with the new input system)? Is there something like GetButtonDown in the new input system?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Starting with version 1.1.1, actions have a WasPressedThisFrame method which basically does this.

The documentation(linked above) includes the following code snippet:

var fire = playerInput.actions["fire"];
if (fire.WasPressedThisFrame() && fire.IsPressed())
    StartFiring();
else if (fire.WasReleasedThisFrame())
    StopFiring();

As the code example shows, there are also WasReleasedThisFrame and IsPressed which are essentially GetButtonUp and GetButton, respectively.

Note that there are special considerations to make that make them behave differently than GetButtonX calls, especially where buttons release and press thresholds are substantially different, or where buttons are pressed and released multiple times in a frame.

1.1.1 IsPressed docs:

This method is different from simply reading the action’s current float value and comparing it to the press threshold and is also different from comparing the current actuation of activeControl to it. This is because the current level of actuation might have already fallen below the press threshold but might not yet have reached the release threshold.

This method works with any type of action, not just buttons.

Also note that because this operates on the results of EvaluateMagnitude(), it works with many kind of controls, not just buttons. For example, if an action is bound to a StickControl, the control will be considered "pressed" once the magnitude of the Vector2 of the control has crossed the press threshold.

Finally, note that custom button press points of controls (see pressPoint) are respected and will take precedence over defaultButtonPressPoint.

1.1.1 WasPressedThisFrame docs:

This method is different from WasPerformedThisFrame() in that it is
not bound to phase. Instead, if the action’s level of actuation (that
is, the level of magnitude — see EvaluateMagnitude() — of the
control(s) bound to the action) crossed the press threshold (see
defaultButtonPressPoint) at any point in the frame, this method will
return true. It will do so even if there is an interaction on the
action that has not yet performed the action in response to the press.

This method works with any type of action, not just buttons.

Also note that because this operates on the results of
EvaluateMagnitude(), it works with many kind of controls, not just
buttons. For example, if an action is bound to a StickControl, the
control will be considered "pressed" once the magnitude of the Vector2
of the control has crossed the press threshold.

Finally, note that custom button press points of controls (see
pressPoint) are respected and will take precedence over
defaultButtonPressPoint.

Be sure to read the documentation for your version for more information.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading