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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Preventing double asignment for keys.

Discussion in 'Input System' started by P3YC1, Apr 5, 2021.

  1. P3YC1

    P3YC1

    Joined:
    Mar 11, 2020
    Posts:
    6
    Hey everyone.
    I've started using the new input system and want to allow my players to assign the hotkeys themselves if they want. So much so good. The problem I'm currently facing is that it is possible to assign a key to multiple actions, which is something I would like to prevent. Is there an efficient way of doing so? Or any way of doing that at all? I was unable to find a way to filter actions by the bindings, nor did I find a premade function for that.

    An example to describe what my problem is:
    Let's say *Attack* is mapped to "Q"
    and *Block is mapped to "E".
    It is now possible to rebind *Attack* to the "E" button, resulting in the "E" Hotkey triggering *Attack* and *Block*.
    I know that I could just use an if condition to prevent both from being executed at the same time, but that does not solve my general problem.

    I'm looking forward to your replies, thanks in advance!
     
  2. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    For a given map, you can get an array of its bindings with the bindings property. Then you could search it to see if the binding you want to use is already in use. But, if you are allowing your player to change binding during play, you might find it easier just to keep your own list and check that. It risks decoupling the actual bindings from what your list contains, but if all your bindings/rebindings/unbindings are done via code you write, it's a risk you can manage.
     
  3. P3YC1

    P3YC1

    Joined:
    Mar 11, 2020
    Posts:
    6
    Is it also possible to still assign the new binding, and unassign the binding set before, that is the duplicate?
    Like:
    Attack is bound to Q.
    Block is bound to E.
    Now the player rebinds Block to the Q-Button.
    This resulting State:
    Attack will have no more binding.
    Block will be bound to Q.
     
  4. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    I always tell my students that a question about what code will do should be answered by running it.

    Let us know what you find out!