Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by McMutton · Nov 09, 2010 at 04:43 AM · inputbuttonmultiplekeyspress

Action executed with more than one key?

Hey guys.

I've been implementing some key command stuff, and have hit a roadblock. I'm wanting to set it up so that the character will perform a dodge action when you hold down Shift and then press one of the directional keys, but I'm not really sure how to do it.

I had tried using a While loop containing an If statement like this:

function Update () {

     while (Input.GetButton("Action"));
         if (Input.GetAxis("Horizontal") != 0) {
             print ("Dodging!");
             }

}

But that makes Unity freeze as soon as Shift (Action) is pressed, as well as interfering with the other command assigned to Shift. What else is there?

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
5
Best Answer

Answer by Jesse Anders · Nov 09, 2010 at 04:57 AM

I think GetButton() and similar functions will only ever return one value in a single update; that is, if it returns true at all, it'll return true every time you call it until the next update occurs. So in your example, once the shift key is pressed, the program will enter an infinite loop that basically equates to 'while (true)'.

Also, it doesn't look like your 'while' loop has a body (you have what's probably an unintentional semicolon after the 'while' statement). I'd also suggest always using brackets to delineate blocks of code (the block of code that I think you intended to associate with the 'while' statement in this case), as it'll make things more clear.

If you want to have one thing happen when the horizontal axis is non-zero and the action key is down and another thing to happen when the horizontal axis is non-zero and the action key is not down, you can just write (untested):

if (Input.GetAxis("Horizontal") != 0) {
    if (Input.GetButton("Action")) {
        // Do one thing
    } else {
        // Do the other thing
    }
}
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 McMutton · Nov 09, 2010 at 02:35 PM 0
Share

Ah, thanks for the fast response. It worked perfectly, too!

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

No one has followed this question yet.

Related Questions

On button press/hold = input from a keyboard? 1 Answer

Ensuring that a button is released before checking that it's pressed? 2 Answers

Input press to play different animation to input hold? 0 Answers

How to set Input Buttons 2 Answers

Why does the keyboard control what button is selected? 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