Search Unity

Custom Keybinding woes....need advice

Discussion in 'Scripting' started by DaveLyons, Jan 19, 2012.

  1. DaveLyons

    DaveLyons

    Joined:
    Jun 20, 2011
    Posts:
    29
    Hi all,

    Started writing a little game and my current task is keybinds. I don't want to use the built in system basically as I think it looks a little shody having that popup window at the start, I plan to have an options menu like every other game under the sun :).

    Currently my system works by monitoring all the keys (using a foreach on the KeyCode enum checking each key every update, not sure why Unity didn't or make the Input.AnyKey just return what keys are being pressed instead but hey ho) then adding the KeyCode to a list. Then some wizardry is applied to turn the keycode list into a string which is then used in a TryGetValue on a dictionary to see what/if this combination has a stored action.

    This works wonders the way I have done it as it basically holds the action in the broadcast event (currently a string but plan to used a start action/stop action event system) while these keys are held down. This system works for movement in the basic tests I have done however I have noticed one huge flaw.

    Only one keybind can be done at once....this means you can run AND jump, you can only run, stop, then jump lol.

    Can anyone think of a different way to do this other than maybe creating my own popup screen at the start, getting the users to define keybinds there and saving them to the standard config file before launching the game (ala oblivion)?
     
  2. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Well, if you store the keys as a KeyCode array..... Then you just if (Input.GetKey(keyCodeArray[thisSpot])) for every keybind.
     
  3. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    You might want to try Event.keyCode.

    Input.GetKeyDown might also work.
     
    Last edited: Jan 19, 2012
  4. DaveLyons

    DaveLyons

    Joined:
    Jun 20, 2011
    Posts:
    29
    mmm good idea however but what happens if you throw in a modifier....
    i.e W is forward (as usual) but Shift + 2 is mapped to something like open backpack. Checking each key would be great if each binding was to single keys.... I guess I could remove the keys from the list that it is checking against once I find a successful bind...
     
  5. DaveLyons

    DaveLyons

    Joined:
    Jun 20, 2011
    Posts:
    29
    That one nearly works.....very close to working infact however there is one vital flaw. Taking the same example from before if you are walking round (holding the 'W' key) then you press the space bar it does indeed make you jump (or atleast trigger the event for jump) however it stops the walking event and doesn't resume it after the space bar is released.

    The hunt continues...
     
  6. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I think youre making this alot more complicated than it needs to be. All movement actions need to be controlled by a set of If/else statements and key states

    Code (csharp):
    1.  
    2.  
    3. int direction = 0;
    4.  
    5. void Update()
    6. {
    7. if(Input.GetKey(KeyCode.Shift)  Input.GetKey(KeyCode.A))
    8.  OpenBackpack();
    9.  
    10. if(Input.GetKey(KeyCode.W)  onGround)
    11.  direction = 1;
    12. else if(Input.GetKey(KeyCode.S  onGround)
    13.  direction = -1;
    14. else direction = 0;
    15.  
    16. if(Input.GetKeyDown(KeyCode.Space)  onGround)
    17.  Jump( direction);
    18.  
    19. }
    20.  
    21. void Jump()
    22. {
    23.  transform.AddVelocity(new Vector3(direction, 3, 0), ForceMode.Impulse);
    24.  onGround = false;
    25. }
    26.  
    27.  
    etc etc
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    nevermind thats not really what you were asking is it... meh..
     
  8. DaveLyons

    DaveLyons

    Joined:
    Jun 20, 2011
    Posts:
    29
    Hehe to be honest though I think you may have made something click. What I was trying to do was have one binding controller so to speak shoving out events. Why don't I just make the individual scripts (i.e the movement script) just do what you have done but in place of hardcoding the keyCode it is looking for, look it up in settings class which contains this dictionary of keybinds instead.

    mmmmmm testing time..
     
  9. DaveLyons

    DaveLyons

    Joined:
    Jun 20, 2011
    Posts:
    29
    I think my day job of coding sometimes takes over this 'fun' side and I sit there trying to follow strict guidelines which sometimes don't lend themselves to coding games lol.
     
  10. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Theres nothing wrong with writing elegant code that handles everything in one function.

    I havnt had to go down the path of sorting out bindings, so I havnt really given it a lot of thought.
     
  11. Artifactx

    Artifactx

    Joined:
    Jul 20, 2012
    Posts:
    14

    Here is a tutorial, that teaches you how to make a menu for setting keybinds:
     
    Makosai likes this.
  12. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    You can advertise in the teaching forum without necroing 3 and a half year old likely-obsolete support questions, just FYI.
     
  13. J0hn4n

    J0hn4n

    Joined:
    May 11, 2015
    Posts:
    24
    I know its old but you can create a Keycode variable like:

    Keycode jump;

    void Start()
    {
    jump = Keycode.A;
    Etc.....

    if(Input.GetKeyDown(jump))
    //Do stufff.....
    }