Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question New Input System: Held on keyboard but toggle on gamepad (sprinting)

Discussion in 'Input System' started by yoziva, Dec 23, 2020.

  1. yoziva

    yoziva

    Joined:
    Jan 26, 2015
    Posts:
    8
    Hello,

    Is it possible without adding any game logic to have:
    - control scheme A: actions are triggered on hold
    - control scheme B: actions are triggered on toggle

    A practical example is the action of sprinting.
    On the keyboard, the player needs to hold shift to sprint.
    On the gamepad, the player needs to press the thumbstick, but he doesn't need to hold it. If he presses again it will stop sprinting (toggle).

    I could check what is the current control scheme to differentiate those 2 cases, but I was wondering if there is a solution by using the interactions.

    I've tried to use 2 actions: StartSprint and StopSprint :

    Code (CSharp):
    1. GameInputs.Character.StartSprint.performed += OnStartSprintTriggered;
    2. GameInputs.Character.StopSprint.performed += OnStopSprintTriggered;
    3.  
    4. private void OnStartSprintTriggered(InputAction.CallbackContext obj)
    5. {
    6.     _sprint = true;
    7. }
    8.  
    9. private void OnStopSprintTriggered(InputAction.CallbackContext obj)
    10. {
    11.     _sprint = false;
    12. }
    On the keyboard, StartSprint has a Press interaction with Press, and StopSprint has a Press interaction with Release. This works.
    But on the gamepad, StartSprint has a Press interaction with Press, and StopSprint has a Press interaction with Press, and both are conflicting.

    Thanks!
     
    Last edited: Dec 23, 2020
  2. yoziva

    yoziva

    Joined:
    Jan 26, 2015
    Posts:
    8
    Here is actually a way to do this with the action setup I was mentioning.
    • On the keyboard, StartSprint has a Press interaction with Press, and StopSprint has a Press interaction with Release.
    • On the gamepad, StartSprint has a Press interaction with Press, and StopSprint has a Press interaction with Press.
    By using pooling instead of events, there is no possible conflicts between the actions :

    Code (CSharp):
    1. public void Update()
    2. {
    3.     if (_sprint)
    4.     {
    5.         if (GameInputs.Character.Sprint.triggered)
    6.         {
    7.             _sprint = false;
    8.         }
    9.     }
    10.     else
    11.     {
    12.         if (GameInputs.Character.Sprint.triggered)
    13.         {
    14.             _sprint = true;
    15.         }
    16.     }
    17. }
    I suppose it would be possible to do this with events too, by subscribing and unsubscribing. But it feels really overkill.
     
  3. yoziva

    yoziva

    Joined:
    Jan 26, 2015
    Posts:
    8
    Actually I find this to be a better solution :

    I use 2 different actions:
    • SprintToggle: Binded on gamepad to Left Stick Press. Not binded on Keyboard.
    • SprintHold: Binded on keyboard to Shift. Not binded on Gamepad.

    Then:

    Code (CSharp):
    1.         GameInputs.Character.SprintToggle.performed += OnSprintTogglePerformed;
    2.         GameInputs.Character.SprintHold.performed += OnSprintHoldPerformed;
    3.         GameInputs.Character.SprintHold.canceled += OnSprintHoldCanceled;
    4.  
    5.         private void OnSprintTogglePerformed(InputAction.CallbackContext obj)
    6.         {
    7.             _sprint = !_sprint;
    8.         }
    9.  
    10.         private void OnSprintHoldPerformed(InputAction.CallbackContext obj)
    11.         {
    12.             _sprint = true;
    13.         }
    14.  
    15.         private void OnSprintHoldPerformed(InputAction.CallbackContext obj)
    16.         {
    17.             _sprint = false;
    18.         }
    This has the advantage of being able to let the player rebind each actions.
     
    alisahanyalcin and BoomStan_ like this.