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
7
Question by icebird1942 · Aug 25, 2013 at 09:10 PM · inputkeycodegetkeypress

Only KeyPress once?

Whenever I use

     if (Input.GetKey(KeyCode.F)){
     Debug.Log("F pressed");
     blah blah
     }

and I press it, it gives the log message like 4 times when I just pressed F once for a really short time. Is there a way to give the message once, no matter how long I hold it down for?

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

Answer by Jamora · Aug 25, 2013 at 09:18 PM

I assume your script is inside the Update method. That method is run every frame. Combine that with the fact that GetKey returns true every frame the key is held down, you will truly have to press the key fast. (You need to keep it down less than about 0.016sec on 60fps.)

To remedy the situation, use Input.GetKeyDown as that is called only when the key is pressed the first time.

Comment
Add comment · Show 4 · 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 xuancoolcat2 · Nov 12, 2015 at 06:05 AM 1
Share

Hi, Im facing the same problem, even I change the method to Input.Get$$anonymous$$eyDown, It still have multi calls when fps is low(less than 20). Can I define a flag to judge only one time of key press in one frame? where can I put this flag? while I m using Fixedupdate()

avatar image kyle-misner-kriel xuancoolcat2 · Aug 03, 2017 at 11:10 PM 1
Share

You could track the last frame or time the key was pressed. This can also be useful if you're trying to control how often a player does an action.

 if (m_lastPressed != Time.time) {
     m_lastPressed = Time.time;
     // Code here.
 }
avatar image sriharshadodda · Jun 08, 2019 at 07:51 AM 0
Share

hey, im using on screen buttons . the same happens when i touch a button ( the statements are run multiple times). Is there any feature similar to "getkeydown" which i can use????????????????????

avatar image hnvasa · Mar 30, 2020 at 04:47 AM 0
Share

Something like this helped...just in case if you are still looking for answer

 void FixedUpdate()
     {
         transform.position = new Vector3(value, transform.position.y, transform.position.z);
 
         if(Input.GetKeyDown(KeyCode.RightArrow)){
             if(value < 2){
                 value += 2;
             }
             
         }
 
         if(Input.GetKeyDown(KeyCode.LeftArrow)){
             if(value > -2){
                 value -= 2;
             }
         }
         
     }

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

Catch Ctrl+Z input in Editor 1 Answer

How do I get a GUI Texture Button to act as a Input Key such as a T button on a key bored or so? 1 Answer

Input.GetKeyDown doesn't work first button press 1 Answer

Unity C# Two keys pressing one after another not doing a function vice versa 0 Answers

gui.button to enable keypress 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