Hi everyone,
I want to create a menu that lists the controls for the player character. That is, keys and the actions they’re bound to. How should I proceed?
Thanks for your patience.
Hi everyone,
I want to create a menu that lists the controls for the player character. That is, keys and the actions they’re bound to. How should I proceed?
Thanks for your patience.
Depending on the design of your game, you can put the key binding information in a specific class or as a static class variable.
public class GameKey {
public static KeyCode Left = KeyCode.A;
public static KeyCode Right = KeyCode.D;
...
}
To use them:
if( Input.GetKey( GameKey.Left ) ) {
//Move your character left
}
To do the binding, there are different ways to do it.
So I will leave that to you to figure it out.
void OnGUI() {
//Standard form
inputString = GUILayout.TextField (...);
GameKey.Left = Enum.Parse(typeof(KeyCode), inputString ); //Convert string to enum
//Press a key form
.... //Some kind of prompt
GameKey.Left = e.KeyCode; //e is the Event.Current;
}
You will also need to save the key binding and load them (this also means that you will load the GameKey
data using a function on the game start). This is a topic I am not familiar with, but I do think that it is doable using PlayerPrefs
.