New Unity Input System Getting continuous input

When I press the button the actions performs only once.
How can I change it so as long as the button is being hold the action would go on?

@absurdnoize
The way I handled this was with a variable. If you are using the Input System, you would need to do a few things:

  • In your Input Asset, add a Hold interaction to your button
  • Create 2 methods in whichever script you are using to receive input
  • 1 method should be subscribed to your button’s performed action and 1 method should be subscribed to the canceled action.

If you are not using the input system, you can follow the same process, but with the old input system, so no need to subscribe delegates to your button actions.

    ...
    using UnityEngine.InputSystem;
    using UnityEngine.InputSystem.Interactions;
    
    public class PlayerController : MonoBehaviour
    {
        private bool isAttackHeld;

        // How many seconds your coroutine will wait before doing its loop again
        private float delay = .1f;

        // When I created my input asset, I exported it as a class so I could
        reference it in other files, so your name may be different from mine
        private InputConfig _inputConfig = new InputConfig();
    
        // Subscribe your methods to the performed & canceled actions on enable
        void OnEnable() {
            _inputConfig.PlayerMap.Attack.performed += handleAttack;
            _inputConfig.PlayerMap.Attack.canceled += handleAttackCancel;
        }
    
        // Unsubscribe your methods to the performed & canceled actions on disable
        void OnDisable() {
            _inputConfig.PlayerMap.Attack.performed -= handleAttack;
            _inputConfig.PlayerMap.Attack.canceled -= handleAttackCancel;
        }
    
        IEnumerator AttackHoldCo() {
            while (isAttackHeld) {
                // Do whatever you need to do
                yield return new WaitForSeconds(delay);
    
                // can also be 'yield return null;' if you want this to loop every frame
                // but that can be taxing on some computers, depending on what you are doing
            }
        }
    
        // This gets called when the action is performed
        // In this case, that means this gets called both when the button is
        // pressed & when the button is held down
        void handleAttack() {
                isAttackHeld = true;
                // Do whatever needs to be done if the button is held down
                StartCoroutine(AttackHoldCo());
            }
        }
    
        // This gets called when the action is canceled
        // In this case, that is when we release the button
        void handleAttackCancel() {
            if (isAttackHeld)
            {
                isAttackHeld = false;
                // Anything else you need to do upon canceling your action
                EndAttack(); // This can be anything, doesn't matter for this explanation
            }
        }
    }

I have mine set up to handle both a press & a hold on the same button, which is why in handleAttackCancel() I first check to see if I am holding the button down before calling my EndAttack() method. If you don’t care about that, then you don’t need that if statement.