I am making a game where I have to input 4-5 buttons in a random sequence every time. There are events in game where you input buttons sequentially. Though each time the game is played, there are different inputs. Is there a way?. Kind of like guitar hero where you make the inputs but with just 4-5 instead of 3-5 minutes. Once the input is done, the event is done.
You can have a data structure like this:
Dictionary<Combo,string> comboStrings = Dictionary<Combo,string>()
{
{Combo.MeleeAttack, "XAAXB"},
{Combo.MegaJump, "AAX"},
{Combo.FastThrow, "XXABB"},
};
–
Combo would be an enum like:
public enum Combo
{
MeleeAttack,
MegaJump,
FastThrow
}
–
Now when you collect input, you add to a size-clamped string, like this – note it doesn’t have to be keyboard-based, e.g. “X” could represent a controller button:
string userInput = "";
void Update()
{
HandleInput();
}
void HandleInput()
{
string oldUserInput = userInput;
if (Input.GetKeyDown("A"))
{
userInput += "A";
}
else if (Input.GetKeyDown("B"))
{
userInput += "B";
}
else if (Input.GetKeyDown("X"))
{
userInput += "X";
}
if (userInput != oldUserInput)
{
CheckComboTrigger();
CapStringLength();
}
}
–
Then in a function like CheckComboTrigger(), you do:
void CheckComboTrigger()
{
foreach (KeyValuePair<Combo,string> comboString in comboStrings)
{
if (userInput == comboString.Value)
{
TriggerCombo(comboString.Key);
userInput = "";
break;
}
}
}
void TriggerCombo(Combo combo)
{
switch (combo)
{
case Combo.MeleeAttack:
// Handle this combo.
break;
case Combo.MegaJump:
// Handle this combo.
break;
}
}
void CapStringLength()
{
const int maxLength = 5;
// Add code to cap your string to a max length.
}
–
You could additionally add code that clears userInput
if ever there’s a certain amount of time passed without any input, meaning combos need to be hit fast. Good luck!