- Home /
Question by
Jcss1462 · Aug 28, 2020 at 10:52 PM ·
buttonbutton trigger eventsevent triggeringevent-handlingpattern
Execute by code a pattern of buttons
Hi, im making a piano videogame and i want to show to the player a pattern of keys that he must to follow in the order correct to pass to the next level.
The keys of the piano are buttons with the event trigger of pointer enter, and i want activate that trigger with c#.
Comment
@Jcss1462 This is a script I have built. Just be sure to input a key matching your sequence on your buttons. Perhap's you can implement a timer and create a second list of floats to see if it alligns with the tempo.
public class PianoScript : $$anonymous$$onoBehaviour
{
// List of Keys in Sequence ascending from 0 up
public List<string> KeySequence;
// Current Position in our Sequence
public int SequencePos;
// Reveals Games current Status
public string Progress;
void Start()
{
// Populating Sequence and setting Progress;
Progress = "In Progress";
KeySequence.Add("A");
KeySequence.Add("S");
KeySequence.Add("D");
KeySequence.Add("F");
KeySequence.Add("A");
KeySequence.Add("S");
KeySequence.Add("D");
KeySequence.Add("F");
}
public void KeyEnter(string key)
{
// Checking if we pressed the correct key or not, and that we have not already failed.
if (key == KeySequence[SequencePos] && Progress != "Failed")
{
if (SequencePos + 1 == KeySequence.Count)
{
Progress = "Completed";
}
}
else
{
Progress = "Failed";
}
// Incrementing our Sequence.
SequencePos += 1;
}
}