P
P
piffo2020-08-21 00:57:58
Unity
piffo, 2020-08-21 00:57:58

How to make the passage of the level using the desired key sequence?

To pass the level, you need to press two keys in a certain sequence. For example, the player is given the following text, +-++-+---+--+-. In this case, plus is the A key, respectively, minus is D. To pass the level, you need to press the keys in the correct order, but if the player presses an unnecessary key, the whole order is reset. What is the easiest way to do this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Ark Tarusov, 2020-08-21
@piffo

Write to the buffer the keys pressed by the player.
For example, at least in list.
When you press the wrong key, you simply reset the list.

G
GFX Data, 2020-08-21
@ShockWave2048

More or less like this. The code didn't run.

int counter = 0;
string target = "adadadddaaa";

void Update()
{
        string input = Input.inputString;

        if (!string.IsNullOrEmpty(input))
        {
            if (input.ToCharArray()[0] == target[counter])
            {
                counter++;
                if (counter >= target.Length)
                { // next level
                }
            }
            else
            {
                // wrong char! restart
            }
        }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question