Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Continue mouse click action on button hold with the input actions asset

Discussion in 'Input System' started by valentingurkov, Mar 17, 2020.

  1. valentingurkov


    Jun 17, 2019

    Pretty much any popular game where you move using your mouse allows holding the button for continuous movement/attack. I am having trouble getting this done with the new input system and I would be very happy getting some advice from you all. I have a simple action in the input actions asset file that fires an event on mouse click:
    Then I get the current mouse position and move my agent across some simple navmesh-ed plane.

    How can I get this to continue executing if I continue to hold my left mouse button? I've tried changing the interaction type to hold from press & release but what it does it just waits to execute the action once if I hold the button long enough. I've seen Mouse.current.leftButton.isPressed in the documentation, but I am looking for a solution within the input actions asset in hopes of making this work better across platforms. My idea also is to expand this behavior not only for movement but to interactions and combat.

    And my second question is, how could I make this work better across platforms? In Diablo 3 for example, you can move the cursor in-game with your gamepad sticks. Can I drop Mouse.current for something more platform-agnostic?

    I'm not asking you to do the code for me, but I would be happy if I can get some advice, I am really liking how this new system works.

    goncalo-vasconcelos likes this.
  2. Rene-Damm


    Unity Technologies

    Sep 15, 2012
    ATM there's no support for continuous callbacks regardless of whether there's input or not. Or put another way, aside from some stuff like timeouts and such, the input system will only do something when input is reported.

    So ATM it's down to handling this in your end of the logic by, for example, doing the processing in Update/FixedUpdate for as long as the button is down.

    What I'd like to see happen in the future is PlayerInput getting support for handling this for you. This way it won't have to sit directly inside actions (further complicating an already complicated system) but it'd still provide all the convenience.
    Kokowolo, TP3 and valentingurkov like this.
  3. valentingurkov


    Jun 17, 2019
    I'd like to thank you for helping me understand what's possible at the moment and what's not. I will try to handle this on my end and will watch how this package is progressing. Thanks again.
  4. brigadestudios10


    Feb 1, 2019

    Did you get the holding mouse down working? I have a work around, but, when you use isPressed and you click on a button the camera drifts. Does that happen to you too?
  5. sarynth


    May 16, 2017
    I hooked into started, cancelled, and performed.

    On started, I set a bool _shouldContinue = true, and _hasStarted = false.
    On cancelled, I set that bool _shouldContinue = false.
    On performed, I set _hasStarted = true. (I do this for my own specific reasons, where I check for the validity of starting the action, such as whether the player is dead before they start moving.)

    Then, in performed, I start the navMeshAgent moving to the clicked location.
    Then, in a LateUpdate(), I check if _shouldContinue && _hasStarted, then I continue to check the cursor location, and update the navMeshAgent's destination.

    This works really well. So a single click moves to the spot, and holding the mouse button continuously moves towards the cursor's location.
    GameDeveloper1111 likes this.
  6. Kokowolo


    Mar 26, 2020
    I understand that the input system does not currently support continuous callbacks, but, out of curiosity, is this going to be a future development, or is there a reason why this feature was left out?
    Drominus likes this.
  7. Ryuuguu


    Apr 14, 2007
    I work around this by using two actions one with press.pressonly to start and one with press.releaseonly to stop.
    Code (CSharp):
    2.         public void OnLeftWheelBrake(InputAction.CallbackContext context) {
    3.             if (context.performed) {
    4.                 wc.leftBrake = context.ReadValue<float>();
    5.             }
    6.         }
    8.         public void OnLeftWheelBrakeEnd(InputAction.CallbackContext context) {
    9.             if (context.performed) {
    10.                 wc.leftBrake = 0;
    11.             }
    12.         }