- Home /
setting up key binding options
At the moment I am trying to figure out how to set up options for setting up key bindings in a game. I want the options to be while the game is playing but I am not sure how to set it up. I am using wasd for controls but I want the player to be able to pick what ever they want as the keys. I was previously using input.getkey(keycode.#) previously and started using input.getkey("#") because I thought I could use some kind of string variable to place in that line but am not entirely sure how to go about doing it or saving that data if the game is restarted.
Here is a tutorial, that $$anonymous$$ches you how to make a menu for setting keybinds: https://www.youtube.com/watch?v=HZVqFIRmdNs
Answer by Vonni · Jan 04, 2013 at 09:34 PM
You could create variables to hold the keybindings.
var kbMoveForward : KeyCode = KeyCode.W;
var kbMoveBackwards : KeyCode = KeyCode.S;
// etc...
// and then put that in the if statement
if(Input.GetKey(kbMoveForward)){
// move forward code...
}
You'd ofcourse need some GUI to change these...
#pragma strict
var kbWalkForward : KeyCode;
function OnGUI () {
var e : Event = Event.current;
if (e.isKey && e.keyCode != KeyCode.None) {
Debug.Log("Detected key code: " + e.keyCode);
kbWalkForward = e.keyCode;
}
}
As for the saving part. You could have a simple text file with keybind info, option settings etc which unity loads information from. ( Not sure exactly how you would do this though ;/)
thanks, The saving part I can figure out later, this will keep me occupied for now.