Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ClementFeanor · Nov 22, 2018 at 03:11 PM · orderkeyscombocheckingpressing

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 !

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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();
     }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ClementFeanor · Nov 22, 2018 at 04:17 PM 0
Share

@Hellium Thanks a lot for the fast and complete answer ! I'll try my best with this one !

avatar image ClementFeanor · Nov 22, 2018 at 04:25 PM 0
Share

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.

avatar image Hellium ClementFeanor · Nov 22, 2018 at 04:30 PM 0
Share

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.

onclick event box

avatar image ClementFeanor · Nov 23, 2018 at 08:30 AM 0
Share

Thanks a lot !

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

97 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges