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?
>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.
This method is different from simply reading the action’s current
floatvalue and comparing it to the press threshold and is also different from comparing the current actuation ofactiveControlto 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 aStickControl, the control will be considered "pressed" once the magnitude of theVector2of the control has crossed the press threshold.Finally, note that custom button press points of controls (see
pressPoint) are respected and will take precedence overdefaultButtonPressPoint.
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 — seeEvaluateMagnitude()— of the
control(s) bound to the action) crossed the press threshold (see
defaultButtonPressPoint) at any point in the frame, this method will
returntrue. 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 aStickControl, the
control will be considered "pressed" once the magnitude of theVector2
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.