Tutorial: let players choose their keys

A controls menu where the player picks the key for Jump, the choice is kept for next time, and one button puts the default back.

1. Build the menu

Build the menu
  1. A UI text named Jump Key shows the current key.
  2. Two buttons: one with the action change jump, one with default keys.

2. The script

The script

Scripts/ControlsMenu.cs

public class ControlsMenu : Behaviour
{
    bool waiting;

    public override void Start()
    {
        // the key chosen last time (PlayerPrefs keeps it between runs)
        string saved = PlayerPrefs.GetString("jump key");
        if (saved != "") Input.Rebind("Jump", saved);
        Show();
    }

    public override void Update(float deltaTime)
    {
        if (UI.Clicked("change jump"))
        {
            waiting = true;
            UI.SetText("Jump Key", "Press a key...");
            return;
        }
        if (UI.Clicked("default keys"))
        {
            Input.ResetBindings();
            PlayerPrefs.DeleteKey("jump key");
            waiting = false;
            Show();
        }
        if (!waiting) return;
        string key = Input.GetAnyKeyDown();
        if (key == null) return;
        // Escape gives up without changing anything
        if (key != "Escape")
        {
            Input.Rebind("Jump", key);
            PlayerPrefs.SetString("jump key", key);
            PlayerPrefs.Save();
        }
        waiting = false;
        Show();
    }

    void Show() { UI.SetText("Jump Key", "Jump: " + Input.GetBinding("Jump")); }
}

Save it as Scripts/ControlsMenu.cs and put it on any object.

3. How it works

How it works

The player presses change jump, then the key they want. The script rebinds the Jump action and stores the choice so it is still there next time the game starts. default keys puts the standard bindings back. Other scripts do not change: they keep asking for the Jump action, whatever key it is on.

Global bindings for the whole project live in Project Settings > input.

Next: make a game from a sentence.