Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by Yharooer · Jul 05, 2013 at 08:31 AM · multiplekeygetkey

Multiple Input.GetKey

Hi,

I want a piece of code which says "if none of the following keys of pressed then..."

So this is what I think is most suitable. (I prefer Javascript).

 if (!(Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))) {
 // Then do what I want.
 }

(I haven't tried it yet but) it's extremely long, and the length is quite annoying. Is there another way to do this, something like this:

 if (!(Input.GetKey(KeyCode.UpArrow, Keycode.W //etc...

So how I could do this?

PS: This is different to if any key is pressed. Thanks!

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 ScriptKid · Aug 16, 2014 at 10:42 AM 0
Share

hey sry for necromancy but i have some issues to get the input right

ich check if left or right is pressed and then let the player move if up is press he jumps if i press lef or right the jump works fine to but somehow not allway i have a seceond function wich le the charakter sprint when i hit left or right and space

no there is a problem if i push right space and up arrow he jumps out of sprinting but not if i hit left space and up arrow :(

on a diffrent cumputer this works but somehow other input stuff is buggy i guess it has something to do with the keyboardbuffer wich is has a overflow or something in this case any idea how to handle this? would be great

5 Replies

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

Answer by Owen-Reynolds · Jul 05, 2013 at 06:48 PM

A few weeks into a regular bring programming class you'd see two ways to handle "none of the above":

 if(Input.GeyKeyDown("a")) {   .... }
 else if(Input.GetKeyDown("b")) { ..... }
 else if ...
 else   // None of the above goes here

If you can't organize things that neatly, the flag trick also works:

 bool keyWasHandled=false;

 if(Input.getKetDown("y")) { ... keyWasHandled=true; }
 // everyone who processes a key should set: keyWasHandled=true

 if(Input.AnyKeyDown && keyWasHandled==false) {
   // we have a key that no one else wanted
 }

The array method (Bunny, above) is "better," but only if you know arrays and, everyone else who's going to mess with your code.

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 Bunny83 · Jul 06, 2013 at 10:04 AM 0
Share

Your first solution won't probably not work for his case ;) It looks like he has the classical WASD and the arrow-keys movement input. So when doing elseif chains only one of the ifs can be executed. The second one will of course work and is in most cases the best solution. It avoids redundancy of keynames. I just answered the question most straight-forward ;)

Actually UT could change the Input.Get$$anonymous$$ey function so it would take a params array since that wouldn't break any existing code

avatar image Yharooer · Jul 06, 2013 at 12:56 PM 0
Share

Well all I want the of statements to do is change a static variable, so this works for me.

avatar image
4

Answer by Bunny83 · Jul 05, 2013 at 01:15 PM

Well, in C# you can create a function with a params parameter. This allows you to pass as much parameters as you like. I'm not sure if that's possible in UnityScript.

This would work in C#

 bool GetAnyKey(params KeyCode[] aKeys)
 {
     foreach(var key in aKeys)
         if (Input.GetKey(key))
             return true;
     return false;
 }

This function could be used like this:

 if (GetAnyKey(KeyCode.UpArrow, Keycode.W /* , ... */ ))

Or for your case

 if (!GetAnyKey(KeyCode.UpArrow, Keycode.W /* , ... */ ))

As general alternative you could define your keys in an array and pass the array to the function.

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 Bunny83 · Jul 05, 2013 at 01:18 PM 0
Share

Sorry, missed the array brackets ^^. fixed it.

avatar image
1

Answer by mentaldepth · May 04, 2015 at 11:50 AM

Here it is working :D thanks for info guys. (very simple but it works all directions no sound cut off)

 var AudioFile : AudioClip;
 
 function Update() {
 
 if ( Input.GetButtonDown( "Horizontal" ) || Input.GetButtonDown( "Vertical" ) )
 {
      GetComponent.<AudioSource>().clip = AudioFile;
      GetComponent.<AudioSource>().Play();
 }   
  
     else if ( !Input.GetButton( "Horizontal" ) && !Input.GetButton( "Vertical" ) && GetComponent.<AudioSource>().isPlaying )
 {
      GetComponent.<AudioSource>().clip = AudioFile;
      GetComponent.<AudioSource>().Stop();
 }
 
 }
 
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 JohnnySunshine · Jul 05, 2013 at 01:02 PM

You could use input axes; by default, you have "Vertical" and "Horizontal" mapped to arrow keys and WASD, which gives you:

 if (Mathf.Approximately(0.0f, Input.GetAxis("Horizontal") && Mathf.Approximately(0.0f, Input.GetAxis("Vertical"))
 {
     // Then you do what you want
 }
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 calegxm999 · Jun 23, 2020 at 06:41 AM

if (Input.GetKey(KeyCode.LeftArrow) && Input.GetKey(KeyCode.RightArrow))

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

21 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

Related Questions

Very wierd issue with right arrow key 3 Answers

gui.button to enable keypress 2 Answers

Returning Which Key Was Pressed 1 Answer

Controlling animation speed 2 Answers

Override windows onaly allowing three buttons being pressed 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