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. Dismiss Notice

Resolved How to set a binding to 'none' when rebinding?

Discussion in 'Input System' started by valentin56610, Aug 12, 2023.

  1. valentin56610

    valentin56610

    Joined:
    Jan 22, 2019
    Posts:
    146
    Hello,

    I would like to allow setting some keybindings to absolutely nothing, but so far, I can't find how to do that.

    I've tried to do a 'EraseBinding()' during the OnCancel() on the rebindOperation, but this does not fully work, as the next time I launch the game the value is back to what it was...

    Yes, I have a fully working save/load system for my keybindings. I know it's not persistent. Problem is, I can't set it to 'nothing'

    Documentation only tells us that:
    Code (CSharp):
    1. // Erase first binding on "fire" action.
    2. playerInput.actions["fire"].ChangeBinding(0).Erase();
    Which is nice, but doesn't save/get written to the override of the keybind...

    So, how does one simply 'override' a native keybind to be 'nothing', and actually be saved with the rest of the overrides?

    Thanks
     
  2. rdjadu

    rdjadu

    Joined:
    May 9, 2022
    Posts:
    102
    Override with an empty string.
     
  3. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    I think (I'm not sure because it was a long time ago when I played around with this and right now I can't test it), but as I remember the JSON serialization will skip it since you remove it.
    Try this instead:
    Code (CSharp):
    1. // Erase first binding on "fire" action.
    2. playerInput.actions["fire"].ChangeBinding(0).ChangeBindingWithPath("");
     
    valentin56610 likes this.
  4. valentin56610

    valentin56610

    Joined:
    Jan 22, 2019
    Posts:
    146
    How do I do that? I'm using the rebinding operation, it doesn't let me choose an empty string, it only listens for keys being pressed, that's my code by the way:

    Code (CSharp):
    1.     void PerformRebindOperation(KeybindingButtonGO p_keybindingButtonGO, InputAction p_inputAction)
    2.     {
    3.         if (_rebindingOperation != null) _rebindingOperation.Cancel();
    4.  
    5.         int index = -1;
    6.  
    7.         // Composite binding, rebinding only one axis at a time
    8.         if (p_inputAction.bindings[0].isComposite) index = p_keybindingButtonGO.GetBindingIndex(_gamePadMode);
    9.         else
    10.         {
    11.             if (_gamePadMode) index = 1;
    12.             else index = 0;
    13.         }
    14.  
    15.         p_inputAction.Disable(); // Critical, you cannot rebind an action that is Enabled, error thrown in console
    16.  
    17.         _rebindingOperation = p_inputAction.PerformInteractiveRebinding()
    18.         .WithTargetBinding(index)
    19.         .WithCancelingThrough("<Keyboard>/escape")
    20.         .WithControlsExcluding("<Mouse>/position")
    21.         .WithControlsExcluding("<Mouse>/scroll")
    22.         .WithControlsExcluding("<Mouse>/delta")
    23.         .OnCancel(cancelOperation =>
    24.         {
    25.             p_inputAction.Enable();
    26.  
    27.             CleanupRebindingOperation();
    28.  
    29.             RefreshVisualsOfAllKeyBindings();
    30.  
    31.         }).OnComplete(completeOperation =>
    32.         {
    33.             p_inputAction.Enable();
    34.  
    35.             CleanupRebindingOperation();
    36.  
    37.             RefreshVisualsOfAllKeyBindings();
    38.  
    39.             StaticClass.SelectObject(null);
    40.  
    41.         });
    42.  
    43.         _rebindingOperation.Start();
    44.     }
     
  5. valentin56610

    valentin56610

    Joined:
    Jan 22, 2019
    Posts:
    146
    Thanks but there is no such possibility, you cannot call 'ChangeBindingWithPath()' on top of / on the result of 'ChangeBinding()'
     
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    You're right, sorry, again, it was a long tme ago. The proper line is
    Code (CSharp):
    1. playerInput.actions["fire"].ChangeBinding(0).WithPath("");
    The problem is, with this, you're overwriting the original binding and this also doesn't show up in the JSON.
    So here is what I figured (I got back in the front of my unity computer):
    Code (CSharp):
    1. playerInput.actions["fire"].ApplyBindingOverride(0, "");
    This will properly show up in the saved JSON AND the opportunity to reset to the original binding remains.
     
    Last edited: Aug 12, 2023
    valentin56610 likes this.
  7. valentin56610

    valentin56610

    Joined:
    Jan 22, 2019
    Posts:
    146
    Well this worked!

    This is the solution, thanks a lot man!
    Code (CSharp):
    1. playerInput.actions["fire"].ApplyBindingOverride(0, "");
     
    Lurking-Ninja likes this.