How to Rebind Your Controls in Unity (With Icons!) | Input System

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 ต.ค. 2024

ความคิดเห็น • 80

  • @mikeha
    @mikeha 6 ชั่วโมงที่ผ่านมา

    this is by far the best input rebinding tutorial for Unity on TH-cam, bar none. I have viewed other tutorials and they are not as robust as this one. This one results in a professional rebinding functionality, suitable for commercial games

  • @manningfamilychronicles1601
    @manningfamilychronicles1601 ปีที่แล้ว +15

    This is no joke right here. Really daunting going through this. Well done. I'm glad I went through this, and I really appreciate the hard work that went into putting this together.

  • @sasquatchbgames
    @sasquatchbgames  ปีที่แล้ว +47

    A bug was found where, if you separate a composite binding (ie. A vector2 up/down/left/right binding) into 4 separate bindings that you want to rebind, a duplication was possible within the composite binding itself.
    To fix this, you'll need to change the CheckDuplicateBindings functions to the following:
    private bool CheckDuplicateBindings(InputAction action, int bindingIndex, bool allCompositeParts = false)
    {
    InputBinding newBinding = action.bindings[bindingIndex];
    int currentIndex = -1;
    foreach (InputBinding binding in action.actionMap.bindings)
    {
    currentIndex++;
    if (binding.action == newBinding.action)
    {
    if (binding.isPartOfComposite && currentIndex != bindingIndex)
    {
    if (binding.effectivePath == newBinding.effectivePath)
    {
    Debug.Log("Duplicate binding found in composite: " + newBinding.effectivePath);
    return true;
    }
    }
    else
    {
    continue;
    }
    }
    if (binding.effectivePath == newBinding.effectivePath)
    {
    Debug.Log("Duplicate binding found: " + newBinding.effectivePath);
    return true;
    }
    }
    if (allCompositeParts)
    {
    for (int i = 1; i < bindingIndex; i++)
    {
    if (action.bindings[i].effectivePath == newBinding.overridePath)
    {
    //Debug.Log("Duplicate binding found: " + newBinding.effectivePath);
    return true;
    }
    }
    }
    return false;
    }
    This also affects the "reset" button, so you'll need to change the ResetBinding function to the below as well:
    private void ResetBinding(InputAction action, int bindingIndex)
    {
    InputBinding newBinding = action.bindings[bindingIndex];
    string oldOverridePath = newBinding.overridePath;
    action.RemoveBindingOverride(bindingIndex);
    int currentIndex = -1;
    foreach (InputAction otherAction in action.actionMap.actions)
    {
    currentIndex++;
    InputBinding currentBinding = action.actionMap.bindings[currentIndex];
    if (otherAction == action)
    {
    if (newBinding.isPartOfComposite)
    {
    if (currentBinding.overridePath == newBinding.path)
    {
    otherAction.ApplyBindingOverride(currentIndex, oldOverridePath);
    }
    }
    else
    {
    continue;
    }
    }
    for (int i = 0; i < otherAction.bindings.Count; i++)
    {
    InputBinding binding = otherAction.bindings[i];
    if (binding.overridePath == newBinding.path)
    {
    otherAction.ApplyBindingOverride(i, oldOverridePath);
    }
    }
    }
    }
    And finally, to ensure you can use either WASD separately OR together, be sure to update the ResetToDefault method to:
    public void ResetToDefault()
    {
    if (!ResolveActionAndBinding(out var action, out var bindingIndex))
    return;
    ResetBinding(action, bindingIndex);
    if (action.bindings[bindingIndex].isComposite)
    {
    // It's a composite. Remove overrides from part bindings.
    for (var i = bindingIndex + 1; i < action.bindings.Count && action.bindings[i].isPartOfComposite; ++i)
    action.RemoveBindingOverride(i);
    }
    else
    {
    action.RemoveBindingOverride(bindingIndex);
    }
    UpdateBindingDisplay();
    }

    • @Storm3532
      @Storm3532 3 หลายเดือนก่อน +2

      This makes it so I can't rebind composites at all, it always says there's a duplicate :/

    • @Malaron2
      @Malaron2 2 หลายเดือนก่อน +4

      @@Storm3532
      Fix Below, explanation: Essentially when it's a composite, the bindingIndex that's passed in is its index within the composite itself, and not within the entire actionMap list of bindings. For example with a WASD composite, bindingIndex is 0 for the header (we don't care about), the first item in the composite will be 1 (W in this case), second item will be 2 (A), and so on. With the currentIndex++ where it's at, it matches our composite binding's index to the ENTIRE list of bindings. Since the elements aren't 1-1 in both lists, it means index will be off and will try to compare itself against itself, finding a 'duplicate'. Moving currentIndex++ down so it only starts incrementing once we've found our composite object will make it so the binding item will start at 0 at that point, it will skip the header (because the header isn't considered "isPartOfComposite"), and then it will compare your composite element to the first item in the composite. The indexing should be correct so it will skip over itself and not have a false duplicate. :)
      foreach (InputBinding binding in action.actionMap.bindings)
      {
      if (binding.action == newBinding.action)
      {
      currentIndex++;

    • @Deatheragenator
      @Deatheragenator 2 หลายเดือนก่อน +1

      @@Malaron2 That fixed that issue.
      Since we're sharing solutions. I discovered that the rebound buttons would only load when I opened the related keyboard or gamepad canvas.
      Solution: I also added the save rebind script to the EventSystem so it loads when the scene is loaded.

  • @leo8292
    @leo8292 6 วันที่ผ่านมา

    What an absolute gold mine of a video. I am about to enter polish phase on my tower defense game and wanted to offer custom key bindings, but the API is just so hard to take full advantage of. You just saved me hours of trial and error. Cheers mate!

  • @Destructivepurpose
    @Destructivepurpose 19 วันที่ผ่านมา

    Bro you are an absolute LIFESAVER! I was banging my head against the wall trying to figure out how to prevent duplicates for hours, and trawling through the code was just getting me more confused! Thanks so much for this video.

  • @karatepunk
    @karatepunk 6 หลายเดือนก่อน +1

    Thank you for your efforts. Your channel is fast becoming my go to place for quality tutorials full of good practices.

  • @trinetrapandey5339
    @trinetrapandey5339 2 หลายเดือนก่อน

    lots of time, lots of sanity saved and lots of features provided, thanks for helping me modify all those high level language programing of unity's prebuilt code

  • @aditya90m
    @aditya90m ปีที่แล้ว +4

    Hey, thanks for the great tutorial. I set up my input system differently, thought I'd share.
    Instead of using the UI based Input Action control binding, I did it all in code. I set up an "InputAction" for each action, and used the "AddBinding or "AddCompositeBinding" function to bind keys/buttons to the action. This way, I can store my control binding map in a json file and then read it and load the controls easily.
    For using these actions, I pass a function definition as an "InputAction.CallbackContext" and add it to action.performed and (optionally) action.canceled. This way, the function gets triggered directly when any action is performed. So, I don't need the bool variables to detect when an action has been performed. The triggered function will receive the context and you can detect whether the action was performed or canceled from the context.
    I wasn't sure out how to do the re-bindings and stuff, but your tutorial has given me a good idea. Thanks again!

  • @plazicx
    @plazicx หลายเดือนก่อน

    This is by far one of the best Unity tutorials that I have ever seen! What would be the right approach if one wants to rebind controls for say 2 players on splitscreen. Let's say we already assigned gamepad controllers for each player and we know which on is which. I just can not figure out how to check during rebinding to accept only the inputs from specific player's controller - and not the other one (since while one player is in rebinding mode - and waiting for the key to be pressed - we do not want to detect keypress from another player's controller). Of course each player needs its own input scheme - even though in their structure they are identical (clones) - but still we need two - that I figured out and that works fine - I just need to prevent rebinding taking inputs from other player's controller. Once again thank you so much for your videos!

  • @eneseren8332
    @eneseren8332 2 หลายเดือนก่อน

    Great tutorial! I needed to use it on my own project, and it works perfectly right now.

  • @Laumania
    @Laumania ปีที่แล้ว +1

    This is one perfect video! Thank you. Saving it for when I’m redoing my ui and will add rebinding :)

  • @benfau9992
    @benfau9992 หลายเดือนก่อน

    Masterclass of a job, well done 🥳

  • @thankyouverymuch5743
    @thankyouverymuch5743 ปีที่แล้ว +1

    Thank you so much, I was searching for this all day.

  • @castlecodersltd
    @castlecodersltd 6 หลายเดือนก่อน +1

    Another really good/helpful tutorial from you. Thank you both 🙂

  • @pocket-logic1154
    @pocket-logic1154 ปีที่แล้ว +2

    Just a tip: You can also press Ctrl + E + C to comment out lines of code, making it a bit easier to reach the keys with one hand in one single motion ;-)

  • @elesh.n
    @elesh.n 5 หลายเดือนก่อน

    This is such a life saving tutorial. Many thanks!

  • @tornadre
    @tornadre ปีที่แล้ว +2

    Thank you for putting this video together. Really helped me out and I really enjoyed your method of tutorial making. I've subbed and will be checking out your other work!

  • @angelrubiov
    @angelrubiov ปีที่แล้ว +1

    Thanks so much for this tutorial, it worked for me and I was extremely lost haha

  • @fleity
    @fleity 5 หลายเดือนก่อน +1

    Very nice, very comprehensive overview of how to so rebinding thank you so much.
    In the beginning you mention rebinding is easier with player input than generated c# classes, but not say why though?

  • @amirhebadatiyaan
    @amirhebadatiyaan 9 หลายเดือนก่อน +1

    Thanks for the great tutorial!!!

  • @karandhir2411
    @karandhir2411 2 หลายเดือนก่อน

    Amazing tutorial, thank you so much!

  • @pengchengliu7648
    @pengchengliu7648 6 หลายเดือนก่อน +1

    This is realy helpful, thank you very much!

  • @matt78324
    @matt78324 ปีที่แล้ว +1

    Awesome video! Subscribed!

  • @AnEmortalKid
    @AnEmortalKid ปีที่แล้ว +2

    For Ui icons, I used Xelu’s controller prompts

  • @AnEmortalKid
    @AnEmortalKid ปีที่แล้ว

    Omg.. i just implemented all of this myself not even knowing there were samples in the Unity package SMH. Learned a thing or two.

  • @treyhayden4261
    @treyhayden4261 ปีที่แล้ว +4

    The duplicate rebind modifications were almost identical to samyam's video. :\

  • @codem7173
    @codem7173 ปีที่แล้ว +1

    Great tutorial, ty

  • @ReidWinchester
    @ReidWinchester 4 หลายเดือนก่อน +2

    the reset function is not working for me. i've double checked my RebindActionUI prefab reset button and it's set to call the resettodefault function when the player clicks, but for some reason, nothing happens when I press the button. I've got the actual rebinding portion of the tutorial done, but the reset buttons, which were apparently supposed to work out of the box, just are not working for me. Any advice? I followed the code for the reset function exactly

    • @Malaron2
      @Malaron2 2 หลายเดือนก่อน

      Did you ever figure this out? I've run into the same issue.

  • @CoderDev6545
    @CoderDev6545 9 หลายเดือนก่อน +1

    I found an issue where if you cancel the rebinding and the binding contains composits, the changes will not get revered and will stay as is. This causes a duplication glitch that I've tried fixing myself, but could not fix. I also found an issue where if you rebind an action, then put in a duplicate, it resets the value back to its original value before the rebinding. However, I found a solution to this error by putting in a variable to remember what is was before the method is performed, so it can revert to that by using "action.ApplyBindingOverride(bindingIndex, _binding);". _binding is the variable that stores the previous value, which you can set before the operation is called: string _binding = action.bindings[bindingIndex].effectivePath;. Can you please help me with the original problem with that being canceling a rebinding that contains composits (which don't revert the changes)? Thank you!

  • @LordBete
    @LordBete 4 หลายเดือนก่อน +1

    Followed every step, but when it comes to testing the rebind, the rebindUI is updated with my new keys but my character still controls using WASD.
    I assume this is because I have generated the C# class you said not to, but I need that class. What do we do in that case?

    • @AscendedJew
      @AscendedJew 29 วันที่ผ่านมา

      I just ran into the same issue. The video didn't really explain it, but you shouldn't need the class for anything. if you followed his tutorial exactly then you should have a "User Input" script.
      Inside of that script make functions to get the input actions you defined and you can use those. Here's my code down below (I've needed to modify mine for other reasons)
      private PlayerInput playerInput;
      private InputAction moveAction;
      private InputAction attackAction;
      private InputAction interactAction;
      private InputAction crouchAction;
      private InputAction jumpAction;
      private InputAction sprintAction;
      private InputAction pauseAction;
      private void Awake()
      {
      DontDestroyOnLoad(this);
      playerInput = GetComponent();
      SetupInputActions();
      }
      private void SetupInputActions()
      {
      moveAction = playerInput.actions["Move"];
      attackAction = playerInput.actions["Attack"];
      interactAction = playerInput.actions["Interact"];
      crouchAction = playerInput.actions["Crouch"];
      jumpAction = playerInput.actions["Jump"];
      sprintAction = playerInput.actions["Sprint"];
      pauseAction = playerInput.actions["Pause"];
      }
      public InputAction getMoveAction()
      {
      return moveAction;
      }
      public InputAction getAttackAction()
      {
      return attackAction;
      }
      public InputAction getInteractAction()
      {
      return interactAction;
      }
      public InputAction getCrouchAction()
      {
      return crouchAction;
      }
      public InputAction getJumpAction()
      {
      return jumpAction;
      }
      public InputAction getSprintAction()
      {
      return sprintAction;
      }
      public InputAction getPauseAction()
      {
      return pauseAction;
      }
      Hope that helps!

  • @davidebertoni719
    @davidebertoni719 19 วันที่ผ่านมา

    You are a real master, thanks a lot for the insights

  • @MasterDisaster64
    @MasterDisaster64 ปีที่แล้ว +4

    Thanks a lot for this!
    Just wondering, how hard would it be to modify this so you can add several bindings to an action instead of just one?

  • @fplayfplay3161
    @fplayfplay3161 11 หลายเดือนก่อน

    Thanks for the tutorial.
    The last part, saving rebinding, getting:
    'InputActionAsset' does not contain a definition for 'SaveBindingOverridesAsJson' and no accessible extension method 'SaveBindingOverridesAsJson' accepting a first argument of type 'InputActionAsset' could be found (are you missing a using directive or an assembly reference?)
    any idea?

  • @sergiovieira4869
    @sergiovieira4869 5 หลายเดือนก่อน

    Great Video! Can you explain why this method of rebinding won't work if you use C# generated class?

  • @chunsh10
    @chunsh10 4 หลายเดือนก่อน

    I can't see Unity.TextMeshPro assembly at 6:31. Do you know how to resolve this?

  • @LilianCRE
    @LilianCRE 3 หลายเดือนก่อน +1

    Hello i'm actualy making a fpv drone video game and i want us to be able to use any radio transmiter. In most of FPV Simulator you have a system call the joystick calibration where you have to move your joystick and the game listen to the input of your controller and bind it. I want to do the same system into my game but i realy don't know how so if somebody know how to do it i will realy apreciat it. Thanx

  • @IAutumnWest
    @IAutumnWest 6 หลายเดือนก่อน

    God bless this video. Absolute hero shit : - )

  • @martinchya2546
    @martinchya2546 ปีที่แล้ว +2

    Hello. The thing in 4:00 technically is not a singleton. Its a global static reference that self-manages. For it to be singleton, it should automatically instantiate itself. Just a minor nitpick.

  • @cedriclaps
    @cedriclaps 2 หลายเดือนก่อน

    In my case after I pressed one button for rebinding its not closing the Rebindingpanel :/ EDIT: found out I missed to return false on duplicate instead I returned also true at the end

  • @stillzen-dev
    @stillzen-dev หลายเดือนก่อน

    is it okay to have the user input script as a singleton even if i have multiplayer?

  • @lacrime_khalil3032
    @lacrime_khalil3032 10 หลายเดือนก่อน

    I need help! Everything works fine but in my game my player's jump height is variable on jump key Being held but so i changed the jump input is userinput but now my player jumps by itself when jump key is held, which is not what i want

  • @yihongzheng1142
    @yihongzheng1142 ปีที่แล้ว +1

    The button binding is successful, but it is not mapped to the character

  • @SoggettoRava
    @SoggettoRava 11 หลายเดือนก่อน

    Hi everyone!
    First of all, great video, it really helped me set up the rebinding options for my game! Still, I need help with a curious issue.
    Since I put all key reset into one single button, I managed to modify CheckDuplicateBinding function to integrate the swapping method you describe. This way, new bindings immediately swap with duplicates, if found. And everything looks like it's working fine.
    Now, I encountered a small issue I'm not sure how to solve, so I'm hoping anyone here could point me in the right direction...
    As I said, rebinding perfectly works as long as I stay in the Editor. When I build the game, however, something weird happens: I can rebind keys without any trouble, but if I close the pause menu (both with escape and by selectin "resume") and then reopen it, the rebind is maintained, but the controls list displays the original, default keys.
    Example: M key opens map; I rebind the function to key G; now the list says MAP - G and if I close menu and test, G key opens map (perfect); now, if I reopen the menu, it says MAP - M (original binding), but the G key is still the one performing the action.
    So, to wrap up, the rebinding works perfectly but it DISPLAYS incorrectly... and only in the build, not in the editor, where new keys are displayed correctly. Any idea on what changes between editor and build? Or on where I should go looking for a solution?
    Thanks!

  • @JagGentlemann
    @JagGentlemann ปีที่แล้ว

    I updated the Input System so everything is on point, but the keyboard stopped working with the Action Map I use for the gameplay. I don't want to do all of this without updating, because I know it'll be worse in the future, and the listen function doesn't even work for me in the old version.

  • @sethdossett1304
    @sethdossett1304 ปีที่แล้ว

    I was actually gonna ask you how you were doing this in your games haha

  • @AlesVokrinek
    @AlesVokrinek 8 หลายเดือนก่อน

    Good tutorial but i for some reason cant rebind like jump to left mouse button. Can you please help me.

  • @benjaminsettlemire7006
    @benjaminsettlemire7006 ปีที่แล้ว +1

    Do I have to use the Input System plugin or can I use the default unity input manager

  • @QWERTZ-NOOB
    @QWERTZ-NOOB 7 หลายเดือนก่อน

    My question is not entirely related to this video but maybe somebody can still shine a light on it for me. Can I use Unity to create a tool that remaps and/or automates controller input? I've made some makros with AHK but I'd rather switch to something more advanced and polished that also uses a C-style language because I don't really like the syntax of AHK and its documentation is very bad. I know that Python is the first choice for tasks like this in the Open Source community but I don't really like that language either.

  • @tommyyodergamecreator
    @tommyyodergamecreator ปีที่แล้ว

    Found a bug and I am not sure how to fix it. Let's say you have one button set to R2 and another set to L2. If you remap the button with R2 to X and proceed to switch the second button to R2. When you go to switch the first button back to R2 the font updates but Waiting For Responses is still visible. It would be neat to add the set of code to swap like when you reset but nothing happens if I add action.RemoveBindingOverride(bindingIndex); to PerformInteractiveRebind during check for duplicates nothing happens. Any advice would be great.

  • @BarelyTryingStudio
    @BarelyTryingStudio 8 หลายเดือนก่อน

    Hopefully you see you this, I was just wondering, if You assigned those sprites for xbox but if the player only has a PlayStation controller wouldn't the player only see xbox controls until they rebind ? is there a way to set the default inputs based on what controller gets detected ?

  • @yihongzheng1142
    @yihongzheng1142 ปีที่แล้ว

    I copy the code step by step and it still can't work in the end ,i don't know why ?😭

  • @wolflexx
    @wolflexx 7 หลายเดือนก่อน

    Thank you

  • @hikineet9673
    @hikineet9673 3 หลายเดือนก่อน

    Thanks

  • @readyok8230
    @readyok8230 ปีที่แล้ว

    Hey there, thanks for this video. I noticed that .WithCancelingThrough ("/escape") will also cancel when you press the "E" key on the keyboard. How would you avoid this? Thanks.

    • @AnEmortalKid
      @AnEmortalKid ปีที่แล้ว

      That’s a unity bug. Lemme find you the code for this one sec.

    • @readyok8230
      @readyok8230 ปีที่แล้ว

      @@AnEmortalKid Hey there, thanks. The solution is to upgrade to input system 1.4.1

    • @AnEmortalKid
      @AnEmortalKid ปีที่แล้ว +1

      @@readyok8230 omg lol. Ok I shall check if I have the latest.

  • @animedreammachine7123
    @animedreammachine7123 ปีที่แล้ว

    Hey man could you do a Steam Deck controls video? Thanks man love the videos

    • @sasquatchbgames
      @sasquatchbgames  ปีที่แล้ว +1

      I need to get myself a Steam Deck first. Thanks for the idea!

  • @barionlp
    @barionlp 5 หลายเดือนก่อน

    Why does no one use UI Toolkit :(

  • @ColtPtrHun
    @ColtPtrHun ปีที่แล้ว

    4:40
    Line 97 I think the walk checking is wrong

  • @flecsodev4210
    @flecsodev4210 6 หลายเดือนก่อน

    Don't forget to change inputs in settings otherwise this doesn't work

  • @fervorzen
    @fervorzen หลายเดือนก่อน

    writing each action in three places is not very good

  • @Coco-gg5vp
    @Coco-gg5vp ปีที่แล้ว

    First

  • @justbake7239
    @justbake7239 7 หลายเดือนก่อน +2

    it really annoys me when I find a good tutorial but it doesn't stick to good programming practices.

    • @Luis-Torres
      @Luis-Torres 6 หลายเดือนก่อน +2

      Most of the code was done by Unity themselves in this video and was edited by Brandon. I'm curious what the issues you had with the programming practices? 🤔

    • @justbake7239
      @justbake7239 6 หลายเดือนก่อน +2

      @@Luis-Torres As much as i would like to explain doing so in a youtube comment is a bit out of scope. For future reference though don't assume that unity always follows good programming practices.

    • @Luis-Torres
      @Luis-Torres 6 หลายเดือนก่อน +11

      @@justbake7239 I don't assume Unity follows best practices...but that's not the point 😅
      Your comment seems to be criticizing the video itself when the reality is that the criticism should be aimed at Unity if at all. If you're going to make a critique about programming practices, then elaborate on that. If you decide not to, then the comment seems kind of pointless to me.
      I was hoping to learn something from this comment.

    • @SekGuy
      @SekGuy 4 หลายเดือนก่อน +1

      ​@@justbake7239well, a lot of the time if it is out of the scope for a comment it is also out of scope for the video...

  • @sebastianhall8150
    @sebastianhall8150 ปีที่แล้ว

    First