- Home /
Keys pressed in specific order
Hi ! I'm quite a beginner in scripting on Unity, but I'm currently working on a short game project for my degree. What I want to achieve : The player has 5 notes (music notes) at his disposal, and he can play them whenever he wants and in any order. However, there are melodies (a series of 5 notes, same note can be used several times) that the player knows, and if he plays notes in the order that corresponds to one of the melodies, it activates its power. For exemple the first one make plants around the player grow. Any idea how i can treat that, staying as simple simple as possible for the beginner that i am ?
Thanks a lot, have a great day !
Answer by Hellium · Nov 22, 2018 at 03:44 PM
The following code has not been tested and may be too complicated or too complete, but will cover your needs. I've commented it to be as clear as possible
// Specify the keycodes in the inspector
public KeyCode[] KeyCodeSeries = new KeyCode[] { KeyCode.Alpa1, KeyCode.Alpha2, KeyCode.Alpha3 } ;
// Use this if you want the user to press the series of keys in a given times interval
public float DelayBeforeReset = 86400; // = One day
// Specify in the inspector the function you want to call
// after the series have been completed
public UnityEngine.Events.UnityEvent OnSeriesComplete;
// Specify in the inspector the function you want to call
// after the series have been failed because a wrong key has been pressed
public UnityEngine.Events.UnityEvent OnSeriesFailed;
// Specify in the inspector the function you want to call
// after the series have been failed because the user did not pressed the key before the end of the delay
public UnityEngine.Events.UnityEvent OnTimeRunnedOut;
// The index in the array of the next key to press in order to continue the series
private int keyCodeIndex ;
// The time (in seconds) the last correct key has been pressed
private float lastKeyPressTime;
private void Update()
{
// Make sure some keys have been specified in the inspector
if( KeyCodeSeries.Length == 0 )
return ;
// Check if the user pressed the key before the end of the timer
if( Time.time - lastKeyPressTime > DelayBeforeReset )
{
keyCodeIndex = 0 ;
if( OnTimeRunnedOut != null )
OnTimeRunnedOut.Invoke();
}
// Correct key pressed!
if( Input.GetKeyDown( KeyCodeSeries[keyCodeIndex] ) )
{
lastKeyPressTime = Time.time;
keyCodeIndex++ ;
// Series completed!
if( keyCodeIndex >= KeyCodeSeries.Length )
{
if( OnSeriesComplete != null )
OnSeriesComplete.Invoke();
// Reset index to allow to start again
keyCodeIndex = 0 ;
}
}
// Wrong key pressed!
else if( Input.anyKeyDown )
{
keyCodeIndex = 0 ;
if( OnSeriesFailed != null )
OnSeriesFailed.Invoke();
}
}
@Hellium Thanks a lot for the fast and complete answer ! I'll try my best with this one !
A quick question, can't figure it out by myslelf : how am I supposed to link a function in the inspector (for the 'On Series Complete()', for exemple) ; do I have to complete the code in order to name the functions and effects, or is it another way ? Sorry if i'm not specific enough, it's a whole new world for me ! But I don't want to bother you more than I already did.
You have to put the given code in a script called $$anonymous$$eySeries$$anonymous$$anager
for instance.
Then, attach the component to the gameObject you want (it can be a simple empty).
Once it's done, you will see in the inspector three boxes similar to the onClick
box when you select an UI button.
Click on the "+" button, drag & drop the gameObject holding the script containing the function you want to call. In the drop down, you will be able to select the desired component and the function to call. This function must be public, and have 0 or 1 argument.
Your answer
Follow this Question
Related Questions
Pressing Keys in a Specific Order 2 Answers
How to check if key is pressed 1 Answer
Combo problems 0 Answers
button sequence command, how to?? 5 Answers
Way to make mouse button input play animation all the way through? 0 Answers