Holding a Button – Multiple interactions on a binding / action

First set up a Player object with a default Play Input script and input actions / bindings.

Open the Input Actions

Add a new Jump action (or anything you want to bind)

Click the + next to Actions and name the action.

Set up the listeners for the action.

On the right open Interactions and click the + to add interactions.

Interactions are different ways you can interact with a action that is part of an input system set of bindings.

In the example above hold and press interactions have been set.

You can use the default settings or customise them (this example will leave them using the defaults.

You can see the name of the variable that these will be linked to

Open your player controller script.

We need to add both the InputSystem and the static (default) InputActions to the class. This gives us access to the InputSystem and the default options easily.

We add an OnJump function that takes a parameter of an InputValue.

Note you use On and then the name of the Action created in the Input Actions file. For example if the action was Crouch you would use OnCrouch.

We get a float from the InputValue to detect how long the button has been held down for.

The first if statement checks if the value is greater than the default hold time. If it is a message “Button Held” is pressed.

Otherwise the else statement is triggered and another if statement is run to see if the value is greater than the default button press point (to check if a button has been pressed).

You could also hard code in values in this point.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using static UnityEngine.InputSystem.InputAction;

public class PlayerController : MonoBehaviour
{

    //PlayerInput inputs;

    // Start is called before the first frame update
    void Start()
    {
      //  inputs = GetComponent<PlayerInput>();
    }

    void OnJump(InputValue ia)
    {
        float val = ia.Get<float>();

        if (val >= InputSystem.settings.defaultHoldTime)
        {
            Debug.Log("Button Held");
        } else
        {
            if(val <= InputSystem.settings.defaultButtonPressPoint)
            {
                Debug.Log("Button tapped");
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

You might also like