简体   繁体   中英

Unity - handle 2 input for keybinding in my game

In my game you can control unit, each unit have spell, and we would like to give to the user the possibility to change the keybinding of his different spell.

The goal is for exemple having the possibility to have a combinaison of input for a spell (exemple : "ctrl + H" send the spell)

I find on the unity Store a plugin named "Rewired" that seem to do that, but it's cost 40€and handle too many feature that I don't want.

So I try to create myself my own script to solve my issue, but I don't know how to create the combination of 2 keycode pressed.

This is my script bellow, do you have any idea on how can I create this ?

KeyCode key;
KeyCode curModifiersKey;(alt, ctrl)
KeyCode nonModifierKey;
KeyCode firstModifierKeyInfo;
KeyCode finalKey;

public void DetectedSeveralInput(KeyCode key)
{
    if (key != KeyCode.AltGr)
    {
        if (key == KeyCode.LeftAlt || key == KeyCode.RightAlt || key == KeyCode.LeftControl || key == KeyCode.RightControl)
        {
            if (modifierPressedCount == 0)
            {
                firstModifierKeyInfo = key;
                modifierPressedCount += 1;
            }
            curModifiersKey = key;
        }
        nonModifierKey = key;
        //finalKey = curModifiersKey + nonModifierKey
        LogVariables();
    }
    else
    {
        Debug.Log("AltGR pressed");
        return;
    }
}

What I do to detect multiple key press is like this

void Update()
{
    bool shiftPressed = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
    bool keyPressed = Input.GetKeyUp( /* other key code here */ );
    if(shiftPressed && keyPressed)
    {
        //Do logic here
    }
}

I check shift key being held with Input.GetKey, but makes sure that the logic only happens once by making the other check an Input.GetKeyUp so it will only be true on key release.

Thanks Tricko for the answer.

But the issue is that i can't do this sytem when you got like 10 units, who have 4 spells. My reference on it is Starcraft 2 where you have a full panel for your own keybinding when you can add mouse button or ctrl/shift/alt also.

That why i want to so something that save the 2 input into one (if it's possible) and relate it to the speel n°X for my unit Y

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM