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
0
Question by aman_jha · Jul 24, 2019 at 09:25 PM · inputinputmanagergamepad

How do I differentiate a button being held down from a simple press?

If I have a gamepad button or a keyboard key, is there a way where I can detect if the player is simply pressing on it vs. holding it?

An example is how, in Super Mario Odyssey, a button press will throw Mario's cap forward, and a button hold will keep it out for a few seconds. How can that sort of detection be done in Unity?

I understand that GetButton() tracks every frame the button is down, but it will still trigger GetButtonDown() at the very beginning, and I'm wondering if there's a way to avoid GetButtonDown() while still getting the hold effect from GetButton(). Is this possible? If not, what are workarounds that games like Mario Odyssey use to create different press/hold actions out of the same button?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Hellium · Jul 24, 2019 at 11:04 PM

The idea is to track the time while the button is held down and trigger an action according to the duration.

 private float holdDuration;
 
 private void Update()
 {
     if( Input.GetKey( KeyCode.Space ) )
     {
         holdDuration += Time.deltaTime ;
     }
     else if( Input.GetKeyUp( KeyCode.Space ) )
     {
         if( holdDuration < 0.2f )
         {
              Debug.Log( "Throw cap" );
         }
         else
         {
              Debug.Log( "Cap held, and thrown" );
         }
         holdDuration = 0;
     }
 }

If you want to prevent a button from being held too long:

 public float MaxHoldDuration = 1;
 public float QuickHoldDuration = 0.2f;
 private float holdDuration;
 
 private void Update()
 {
     if( Input.GetKeyDown( KeyCode.Space ) )
     {
         holdDuration = 0 ;
     }
     if( holdDuration >= 0 && Input.GetKey( KeyCode.Space ) )
     {
         holdDuration += Time.deltaTime ;
         if( holdDuration >= MaxHoldDuration )
         {
              Debug.Log("Strongly throw the cap");
         }
     }
     else if( Input.GetKeyUp( KeyCode.Space ) )
     {
         if( holdDuration < QuickHoldDuration )
             Debug.Log( "Throw cap" );
         else
             Debug.Log("Strongly throw the cap");
         holdDuration = -1;
     }
 }
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 crlycstl · Jul 24, 2019 at 11:04 PM

What about starting a timer when the user begins holding down the button, and then checking on GetButtonUp() whether the button was held down long enough to perform the held-down action?

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

198 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 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

New Input System not detecting PS4 Gamepads 0 Answers

How to disable Vive Controllers 5.5 support 1 Answer

Xbox360 Trigger Axis not recognized 0 Answers

Perform Action Only When Button is Pressed (Gamepad with Input System) 1 Answer

Gamepad.current.SetMotorSpeed() doesn't work on mobile! 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