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
1
Question by m0nkeybl1tz · Apr 23, 2014 at 11:33 PM · inputbuttoncoroutinegetbuttondown

Using a coroutine to wait for a button press?

One of the items in the game I'm working on allows you to control the wind. The way it works is you press a button to select the wind power, then the game should continue as normal while it waits for you to specify what direction you want the wind to go in. I figured a coroutine would be the best way of going about it, but for some reason once one of the directional buttons is pressed, the coroutine ends without changing the wind direction. Any help would be greatly appreciated!

     IEnumerator Wind(){
 
         do {
 
             if(Input.GetButtonDown("Horizontal")){
                 windDir = new Vector3(10*Input.GetAxis("Horizontal"),0,0);
                 isWindy = true;
             }
             
             if(Input.GetButtonDown("Vertical")){
                 windDir = new Vector3(0,0,10*Input.GetAxis ("Vertical"));
                 isWindy = true;
             }
 
             yield return null;
         } while (!Input.GetButtonDown("Horizontal") && !Input.GetButtonDown("Vertical"));
 
     }
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
0
Best Answer

Answer by Owen-Reynolds · Apr 24, 2014 at 12:53 AM

Not sure a coroutine is the best place to check for buttons. Seems too easy to double-read the same button in the normal Update check.

For yours, look at the loop structure. The end and the beginning of all loops always touch. So yours waits for a frame (the yield), quits if a button was pressed, then would process the button. Move the yield to the start of the loop?

But, GetAxis ramps up for how long the button was held. Checking it immediately after the button goes down will give a value close to 0. Unless they held it for a while, let go, and pressed it quickly before it drops much. Seems like maybe you want to wait until they release it, then check getAxis.

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 m0nkeybl1tz · Apr 24, 2014 at 03:29 PM 0
Share

Ah, that totally makes sense. I've just started using coroutines, and I'm still getting used to their logic. $$anonymous$$oving the yield to the top made it work, but do you think it would still be better to move it to the update function? Also, I didn't realize that GetAxis had a sliding scale. I just assumed it was -1 or 1... good to know!

avatar image Owen-Reynolds · Apr 24, 2014 at 10:04 PM 0
Share

$$anonymous$$y thinking is: Update is already checking for buttons. If this coroutine runs, Update's button check will have to add "if the coroutine isn't running... ." Then maybe some other button cancels wind selection, so it has to kill that coroutine. Ick. Easier to just say waitingForWindDir=false;

GetAxis is a special helpful thing for new users. It's a secret extra var. As you hold the button, it creeps up to 1 (or -1.) When you let go, it falls. It's made specially for smooth characterController speed-up and slow-down.

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

Wait for button response in coroutine? 2 Answers

shift key input script 6 Answers

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

Is there any function to check how long a button is pressed? 2 Answers

The "GetButtonDown Version" of GetAxis for Analog Sticks 0 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