2

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;
    }
}

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Actually, keyPressed should be Input.GetKeyDown(/* something */), because you want action to be executed at the time the key is pressed, not released.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.