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
0
Question by Levantez · Feb 01, 2014 at 09:42 AM · inputbuttonkey pressed

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

For example, let say that there is an Attack Button.

If the player make a slight tap [ immediately release after press ], then the Player will do Light Attack

a long tap [ hold for a bit then release ], then the Player will do Heavy Attack

a hold [ Not release after a certain amount of time ], then the Player will do Charging


I understand that this can be achieve by

timer++ during Update()

and

timer = 0 when buttonUp()


What I would like to know is,

Is there any Unity Input function that detect how long the button was pressed,

instead of coding a separate timer?

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

Answer by GTX Titan · Feb 01, 2014 at 10:45 AM

I would do this when button is pressed: timer += Time.deltaTime; and when it's not i would do this: ... //some code timer = 0; //re-initialize

Comment
Add comment · Show 3 · 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 Owen-Reynolds · Feb 01, 2014 at 06:46 PM 0
Share

Same idea, but I think easier:

On the press set your variable downTime=Time.time;. Then on release look at Time.time-downTime, which is the hold-time in seconds. Essentially, write down the time they press, then look at the clock and subtract when they let go.

avatar image GTX Titan · Feb 01, 2014 at 10:13 PM 0
Share

I didn't realize you could do that :D thx might help me someday But you should add some variable to check if it's the first time the button is down... otherwise it would record a new downTime each frame and on release you would get what Time.deltaTime would give you...

avatar image Levantez · Feb 02, 2014 at 12:29 AM 0
Share

Thank you Owen, that is the best method I've seen thus far.

To GTX Titan, Unity already have ButtonDown, which will get call only once when button got pressed, so that should come in handy.

avatar image
0

Answer by getyour411 · Feb 01, 2014 at 09:57 AM

No, there are various methods, events, timers, while/loop increments, etc but no Input.HoldDownFor()

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

20 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

Related Questions

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

How can I make GUI button send an input message when I press it? 0 Answers

Input.getTouch() vs checking if button is clicked 1 Answer

Why does the keyboard control what button is selected? 2 Answers

How to set Input buttons from the script? 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