0
\$\begingroup\$

I have created a default key map for my game, however I have also included an option in the settings where the player can change the key bindings.

My issue is if the player clicks on the button for shooting, which is defaults to mouse0, or aim on mouse1, then click on another key, they can never go back to mouse0 or mouse1.

I am unsure of how to include all the mouse buttons/scroll wheel into the viable buttons the player can choose from.

Here is the part of my code that deals with the changing of the keys:

private void OnGUI()
{
    if(curKey != null)
    {
        Event e = Event.current;
        if (e.isKey)
        {
            keyBinds[curKey.name] = e.keyCode;
            curKey.transform.GetChild(0).GetComponent<Text>().text = e.keyCode.ToString();
            curKey.GetComponent<Image>().color = normal;
            curKey = null;
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ Can you show us the relevant part of the script you're using to detect the control pressed for the remapping? We may be able to show you how to extend that code to work with the mouse. Without it, we have to guess about how your current code works. \$\endgroup\$ – DMGregory Aug 9 at 11:44
  • \$\begingroup\$ You probably noticed that code formatting is not very legible in comments. When folks ask you for more information, that information should always be added to your question itself, using the edit button. Then we can clean up comments that aren't needed anymore, rather than keeping a long back-and-forth comment thread folks have to read through to glean all the important info. \$\endgroup\$ – DMGregory Aug 9 at 16:40
  • \$\begingroup\$ Thank you @DMGregory i did notice and I have changed my original post to include the code. \$\endgroup\$ – Sajomis Aug 9 at 16:59
0
\$\begingroup\$

First you'll need to extend your input binding map to handle things that aren't key codes, something like this:

public enum BindingType {
    Unset,
    Keyboard,
    MouseButton,
    MouseScroll
}

[System.Serializable]
public struct InputBinding {

    public BindingType type;

    public KeyCode key;

    public int mouseButton;

    public Vector2 scrollDirection;

    public InputBinding(KeyCode key) {
        type = BindingType.Keyboard;
        this.key = key;
        mouseButton = -1;
        scrollDirection = Vector2.zero;
    }

    public InputBinding(int mouseButton) {
        type = BindingType.MouseButton;
        key = KeyCode.None;
        this.mouseButton = mouseButton;
        scrollDirection = Vector2.zero;
    }

    public InputBinding(Vector2 scrollDirection) {
        type = BindingType.MouseScroll;
        key = KeyCode.None;
        mouseButton = -1;
        this.scrollDirection = scrollDirection;
    }

    public bool Fire() {
        switch(type) {
            case BindingType.Keyboard:
                return Input.GetKeyDown(key);
            case BindingType.MouseButton:
                return Input.GetMouseButtonDown(mouseButton);
            case BindingType.MouseScroll:
                return Vector2.Dot(Input.mouseScrollDelta, scrollDirection) > 0f;
        }
        Debug.LogError("Looks like you added a new input type and forgot to wire up the Fire() method.");
        return false;
    }

    public override string ToString() {
        switch(type) {
            case BindingType.Keyboard:
                return key.ToString();
            case BindingType.MouseButton:
                switch(mouseButton) {
                    case 0: return "Primary Click";
                    case 1: return "Secondary Click";
                    case 2: return "Middle Click";
                    default: return "Error - Invalid Mouse Button";
                }
            case BindingType.MouseScroll:
                if (scrollDirection.y > 0)
                    return "Mouse Scroll Up";
                if (scrollDirection.y < 0)
                    return "Mouse Scroll Down";
                if (scrollDirection.x > 0)
                    return "Mouse Scroll Right";
                if (scrollDirection.x < 0)
                    return "Mouse Scroll Left";
                return "Error - Invalid Mouse Scroll";
        }
        return "Error - Invalid Input BindingType";
    }
}

Dictionary<string, InputBinding> inputBindings = new Dictionary<string, InputBinding>();

You could also do this with polymorphism rather than a union, but the way shown here lets you manipulate these bindings in the Unity inspector, which doesn't handle serialization of polymorphic types as well as we might like. Note that you could compact the storage of this type by overlapping the fields, since they're mutually exclusive in use.

Then you can break out your search for an input:

InputBinding CaptureFirstInput() {

    Event e = Event.current;

    if (e.isKey && e.keyCode != KeyCode.None)
        return new InputBinding(e.keyCode);

    for (int i = 0; i < 3; i++)
        if (Input.GetMouseButtonDown(i)) 
            return new InputBinding(i);

    if(Input.mouseScrollDelta != Vector2.zero)
        return new InputBinding(Input.mouseScrollDelta);

    return default(InputBinding);
}

And apply the return value to your input mapping:

private void OnGUI()
{
    if(curKey != null)
    {
        var input = CaptureInput();
        if(input.type != BindingType.None) {
            inputBindings[curKey.name] = input;
            curKey.transform.GetChild(0).GetComponent<Text>().text = input.ToString();
            curKey.GetComponent<Image>().color = normal;
            curKey = null;
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ Hey, it worked perfectly, I had to rewrite a little bit of it to work for my existing code, thank you for the help! \$\endgroup\$ – Sajomis Aug 26 at 7:03
0
\$\begingroup\$

I presume the flow for setting the key is to click the box of the action and then pressing the key the player wants to bind to it.

You can also listen to mouse clicks in that time and use the first action that is performed.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.