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 /
This question was closed Aug 11, 2017 at 04:39 PM by Igor_Vasiak for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Igor_Vasiak · Jun 02, 2017 at 12:07 AM · scripting probleminputarrayif-statementskeycode

Check if pressed key exists inside an array of KeyCodes

Is there anything like this for KeyCode?

  using UnityEngine;

  void LateUpdate ()
  {
       KeyCode[] keyCheck = new KeyCode[4];
       if (Input.anyKeyDown == keyCheck[2])
            Debug.Log(Input.inputString);
  }
Comment
Add comment · Show 1
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 Lycanthope · Jun 02, 2017 at 12:43 AM 0
Share

this?

 void LateUpdate ()
 {
        foreach ($$anonymous$$eyCode item in keyCheck )
        {       
               if (Input.Get$$anonymous$$eyDown(item))
                      Debug.Log(Input.inputString);
        }
 }

2 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by Addyarb · Jun 02, 2017 at 03:25 AM

You may be looking for something like this. Please be sure to change the declaration (the first line) to the Start function!

     void Update()
     {
         KeyCode[] keycodes = new KeyCode[]{ KeyCode.W, KeyCode.A, KeyCode.S, KeyCode.D };
         for (int i = 0; i < keycodes.Length; i++) if (Input.GetKeyDown(keycodes[i])) Debug.Log("Pressed " + keycodes[i]);
     }
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 Igor_Vasiak · Jun 02, 2017 at 03:36 AM 0
Share

What do you mean by "...change the [...] first line [...] to Start function!"? And, by the way, that worked, thank you!

avatar image Addyarb · Jun 02, 2017 at 04:13 AM 2
Share

Good question. So this line:

 $$anonymous$$eyCode[] keycodes = new $$anonymous$$eyCode[]{ $$anonymous$$eyCode.W, $$anonymous$$eyCode.A, $$anonymous$$eyCode.S, $$anonymous$$eyCode.D };

Is relatively 'expensive,' or in other words - it will cause a significant lag in your game if you keep it in the 'void Update' function/method.

To solve this, move it to the top of your script. You can define the keys you want pressed there, and not have to tell your script every frame. The reason is that it only needs to be called once, whereas the script should check every frame (hence Update) whether or not you are pressing one of those keys.

Your final script, therefore, should look like this:

     $$anonymous$$eyCode[] keycodes = new $$anonymous$$eyCode[]{ $$anonymous$$eyCode.W, $$anonymous$$eyCode.A, $$anonymous$$eyCode.S, $$anonymous$$eyCode.D };
 
     void Update()
     {
         for (int i = 0; i < keycodes.Length; i++) if (Input.Get$$anonymous$$eyDown(keycodes[i])) Debug.Log("Pressed " + keycodes[i]);
     }
avatar image Igor_Vasiak Addyarb · Jun 02, 2017 at 11:51 AM 0
Share

Oh you meant that... As I said in a comment at the answer below, I was way too hurried to think about code shaping, so I wrote it the fastest legible way... Sorry about that, but, anyway, thanks, it was clearifying!

And about the Update thing, I keep alternating, at some scripts I use Update, and at others I use LateUpdate.

Again, thanks!

avatar image Igor_Vasiak Addyarb · Jun 02, 2017 at 12:48 PM 0
Share

@Addyarb Please, check this question, 'cause I guess you might have an answer to it: http://answers.unity3d.com/questions/1360956/trouble-with-fieldinfosetvalue-and-arrays.html

avatar image
0

Answer by Brogan89 · Jun 02, 2017 at 01:49 AM

Right now you are creating an array of KeyCode types. Which has been initialised but not populated. If I understand what you are trying to do i think you'd need to do this

     KeyCode[] keyCodes = new KeyCode[]
     {
         KeyCode.A,
         KeyCode.B
     };

     if (Input.GetKeyDown(keyCodes[2]))
     {
         Debug.Log(Input.inputString);
     }
Comment
Add comment · Show 1 · 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 Igor_Vasiak · Jun 02, 2017 at 03:45 AM 0
Share

That declarations above, in my example, are just what they are: EXA$$anonymous$$PLES. I did it that way 'cause I was way to busy to keep pressing space to format my text. If I were at Visual Studio, that's what they would look like:

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;

  public class $$anonymous$$yExample : $$anonymous$$onoBehaviour
  {
       [SerializeField] private $$anonymous$$eyCode[] keyCheck = new $$anonymous$$eyCode[4]; //The keys are set at Inspector

       void LateUpdate ()
       {
            if (Input.any$$anonymous$$eyDown == keyCheck[2]) //Now I know what is wrong with this declaration, so never $$anonymous$$d.
                 Debug.Log(Input.inputString);
       }
  }

Anyway, thanks for spending your time to answer me!

Follow this Question

Answers Answers and Comments

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

Related Questions

Trying to change character skins at the push of a button. C# 1 Answer

Press button once to enter vehicle and once again to exit 1 Answer

How come my camera does not follow my script? 1 Answer

How not to add an object to an array/list if there's already an identical one. 2 Answers

"Mouse Scrollwheel" on xbox 360controller 2 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