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 /
  • Help Room /
avatar image
1
Question by Mokenister · Feb 23, 2016 at 06:06 AM · c#movementinput.getkey

c# - Make something happen if I press a sequence of keys

How can I make for example my player run faster (which would be speed = speed + 15;) after I press a sequence of keys, not at the same time but one after another? For example, if (Input.GetKeyDown (KeyCode.C, KeyCode.B, Keycode.P)); {speed = speed + 15;} ? That doesn't work so I need someone to help me do this Thank you in advance

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

4 Replies

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

Answer by NoseKills · Feb 23, 2016 at 07:18 AM

 private KeyCode[] sequence = new KeyCode[]{
 KeyCode.A, 
 KeyCode.B,
 KeyCode.C};
 private int sequenceIndex;
 
 private void Update() {
     if (Input.GetKeyDown(sequence[sequenceIndex])) {
         if (++sequenceIndex == sequence.Length){
              sequenceIndex = 0;
              // sequence typed
          }
     } else if (Input.anyKeyDown) sequenceIndex = 0;
 }

That should give you an idea. Written on my phone so I don't have intellisense etc. so it might have typos/syntax errors :-/

Comment
Add comment · Show 2 · 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 rushikesh988 · Feb 23, 2016 at 07:24 AM 1
Share

Nice !! Short and Simple.

avatar image Jackstein · May 03, 2020 at 07:35 PM 0
Share

Works like a charm. Well done!!!

avatar image
1

Answer by rushikesh988 · Feb 23, 2016 at 06:53 AM

@Mokenister Following code might help you .

 using UnityEngine;
 using System.Collections;
 using System.Diagnostics;
 
 public class KeySequenceDetector : MonoBehaviour {
 
     bool KeyCPressedBefore = false, KeyBPressedBefore = false;
     void Start()
     {
       
     }
     void Update()
     {
         if (Input.anyKeyDown)
         {
             if (Input.GetKey("c") || KeyCPressedBefore)
             {
 
 
                 if (Input.GetKey("c"))
                     KeyCPressedBefore = true;
                 else {
 
                     if (Input.GetKey("b") || KeyBPressedBefore)
                     {
                         if (Input.GetKey("b"))
                             KeyBPressedBefore = true;
                         else {
                             if (Input.GetKey("p"))
                             {
                                 //Do Something After Detection of Sequence
                                 UnityEngine.Debug.Log("Sequence Detected of c-b-p");
                             }
                             else
                             {
                                 KeyBPressedBefore = false;
                                 KeyCPressedBefore = false;
                             }
                         }
 
                     }
                     else
                     {
                         KeyBPressedBefore = false;
                         KeyCPressedBefore = false;
                     }
                 }
             }
             else
             {
                 KeyCPressedBefore = false;
 
 
             }
         }
     }
    
 }
 
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
1

Answer by EmHuynh · Feb 23, 2016 at 07:41 AM

Hello, @Mokenister.

After reading your question, I wrote a class to handle the detection of key sequence.

Here is KeySequence.cs:

 using UnityEngine;
 using System.Collections;
 
 [ System.Serializable ]
 public class KeySequence
 {
     public         KeyCode[]    sequence;
     private        int         _sequenceIndex;
     private        bool         _isDetected;
 
     public bool IsDetected() { return _isDetected; }
 
     public KeySequence() { sequence = new KeyCode[ 0 ]; }
 
     public KeySequence( KeyCode[] seq ) {
         sequence = new KeyCode[ seq.Length ];
         seq.CopyTo( sequence, 0 );
     }
 
     public bool Check()
     {
         if( sequence.Length > 0 )
         {
             if( Input.GetKeyDown( sequence[ _sequenceIndex ] ) ) { _sequenceIndex ++; }
             else if( Input.anyKeyDown ) { _sequenceIndex = 0; }
 
             _isDetected = ( _sequenceIndex == sequence.Length ) ? true : false;
 
             if( _isDetected ) { _sequenceIndex = 0; }
         }
         else { _isDetected = false; }
 
         return _isDetected;
     }
 }

KeySequence.cs file: http://bit.ly/1pivoe2

Usage example: http://bit.ly/1QdT9g8

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 leodluz · Mar 04, 2018 at 09:00 PM

https://forum.unity.com/threads/bsd-button-sequence-detector-combos.520051/

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

105 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using a class to control two "player" objects 1 Answer

How to make 2 different actions for the same input? 2 Answers

(Newbie's First Project) Object doesn't stop moving until long after I let go of the button 1 Answer

Regarding transform.position in the roll a ball tutorial 0 Answers

How To Move My Player With Sin And Cos Functions 1 Answer


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