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 howonn · Dec 18, 2017 at 06:03 PM · score systemkeypress

Scoring system by pressing a series of particular keys

( I'm sorry for my bad English and lack of ability.. :C )

I want to make some scoring system that goes up when users press a series of particular keys.

e.g. they press 'A' - 'S' - 'D' - 'F' in order, and then they score.

If they get it wrong in the middle, they should start pressing 'A' again.

How do I make it? Thank you for hearing my problem !!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Prastiwar · Dec 18, 2017 at 08:37 PM

It's my suggestion:

 [SerializeField] KeyCode[] keys; // to make sequence in unity editor
 void Start()
     {
         StartSequence(keys);
     }
 void StartSequence(KeyCode[] keys)
     {
         StartCoroutine(WaitForInput(keys, 0)); // start reading first key from sequence array
     }
 
     IEnumerator WaitForInput(KeyCode[] key, int index)
     {
         while (!Input.anyKeyDown) // wait for key pressing
         {
             yield return null;
         }
         if (Input.GetKeyDown(key[index])) // if correct key is pressed
         {
             if (index + 1 >= key.Length) // check if is there any key read, if no - give score
                 EndOfSequence(); // random void where you can put score funcionallity
             else
                 StartCoroutine(WaitForInput(keys, index + 1)); // go for next key in array sequence
         }
         else // if it's bad key, return to first key of sequence.
         {
             StartCoroutine(WaitForInput(keys, 0));
         }
     }
Comment
Add comment · 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
0

Answer by KyleRasmusson · Dec 18, 2017 at 09:46 PM

Here's my suggestion. Code compiles, runs and will reset if you hit the wrong key.

We do this by creating an array of keys, and an index integer to hold our place in the array. When we hit a correct key, we increment through the array. If we hit a wrong key, we revert back to place 0, the beginning of the array.

In the inspector panel, you can create as many key inputs to run through as you want.

     //Our keys to use
     public KeyCode[] Keys;
 
     //Integer used to index the array
     int Check;
 
     // Update is called once per frame
     void Update()
     {
         InputCheck();
     }
 
     void InputCheck()
     {
         //Making sure we don't go above the number of keys (preventing index out of range issues)
         if (Check <= Keys.Length - 1)
         {
             //If we pressed down the right key, increase
             if (Input.GetKeyDown(Keys[Check]))
             {
                 Debug.Log("CORRECT KEY!");
 
                 //Increase check, so we check against the next key
                 Check++;
 
                 //If we've hit all keys successfully
                 if (Check == Keys.Length)
                 {
                     //Score increase!
                     Debug.Log("ADD SCORE!");
                 }
             }
             //If we pressed down a key, and the key is not the correct key
             else if (Input.anyKeyDown && !Input.GetKeyDown(Keys[Check]))
             {
                 Check = 0;
 
                 Debug.Log("WRONG KEY!");
             }
         }
     }
Comment
Add comment · 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

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

73 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

Related Questions

What key was pressed? 4 Answers

Teleportation 1 Answer

Should character direction come from key press, or third person controller vector3? 0 Answers

make an object explode 3 Answers

Is it possible to have hotkeys for characters like å, ä, ö? 3 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