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 /
avatar image
0
Question by bracciata · Sep 20, 2015 at 11:17 PM · c#lagslowkeypressmethods

Method for checking if a key is pressed without slowing game.

I have had my scripts as

 void update()
 {
  if(input.key...){do bla bla}
 }

for every key but I felt it was greatly slowing down the game having all of these ifs for keyup and key down. Is there a more efficient way such as a method that's called when a key is pressed.

Comment
Add comment · Show 4
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 jmgek · Sep 20, 2015 at 11:52 PM 1
Share

I'll explain what's going on here. The reason why it's "Slowing" your frames is because every frame it's checking to see if you have pressed a key. I can imagine if it's slowing your game then you have a bunch of if statements. The best way to handle this is with a simple manager / case statment. First check if a key was pressed then send it off to a method as an argument.

avatar image bracciata jmgek · Sep 21, 2015 at 12:59 AM 0
Share

Thanks I will do that.

avatar image bracciata jmgek · Sep 21, 2015 at 01:22 AM 0
Share

Hey is there any method or trick for keyup

avatar image jmgek bracciata · Sep 21, 2015 at 02:36 AM 0
Share

You mean Get$$anonymous$$eyUp();

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Xarbrough · Sep 21, 2015 at 12:45 AM

It's very unlikely that if statements are slowing down your game. You could check for Input.GetKey hundreds of times per frame and wouldn't notice it much unless something else is going on. You should use the Profiler, fps counters or the C# Stopwatch class to measure performance if you feel you're getting stuttering. But the if statement itself is one of the cheapest operations. There might be something else involved like expensive calculations when comparing etc., but that's why we have the Profiler.

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
1

Answer by drex150 · Sep 21, 2015 at 02:23 AM

You can just use the following:

 if (Input.anyKey)
 {
    //do whatever here.
 }

http://docs.unity3d.com/ScriptReference/Input-anyKey.html

Comment
Add comment · Show 5 · 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 bracciata · Sep 21, 2015 at 02:27 AM 0
Share

Do you know of way to check if any keys are up

avatar image Bunny83 bracciata · Sep 21, 2015 at 02:38 AM 0
Share

If you mean you want to detect any keyup event, then yes, there is a way: OnGUI and the Event class.

 void OnGUI()
 {
     Event e = Event.current;
     if (e.type == EventType.$$anonymous$$eyUp)
     {
         Debug.Log("User released key: " + e.keyCode)
         // e.keyCode contains the key code of the released key
     }
 }

avatar image drex150 bracciata · Sep 21, 2015 at 02:41 AM 0
Share

Hmm, I don't think so. The only way you could really check if any individual key is up or down is to check that key specifically. So you'd have to check for every key possible if it's up or down.

A way to prevent it from checking every single frame if every single key is pressed would be to put all the keys inside that Input.any$$anonymous$$ey if statement like so:

 if (Input.any$$anonymous$$ey)
         {
             if (Input.Get$$anonymous$$ey("i"))
             {
                 print("i worked!");
             }
             if (Input.Get$$anonymous$$ey("j"))
             {
                 print("j worked!");
             }
         }

Now it will only check what key was pressed when any key is actually pressed. This would only help slightly though because any time a key is pressed and held down, it's going to check all the other keys every frame again until you are pressing no keys.

What exactly are you trying to have happen in your game? That could help possibly.

avatar image bracciata drex150 · Sep 21, 2015 at 02:46 AM 0
Share

It's a 2d side scroller and the input.getkey down make a bool true which will invokerepeating method that controls movement and on key up makes it false and cancels the method.

Show more comments

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

31 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

Related Questions

Infinite Terrain Dilemma. (Chunk Based System... Not A Minecraft Clone) [FIXED! PLEASE USE AS A REFERENCE.] 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unity 2020.3.21f1 running slow 2 Answers

Why is the paralaxing shaky? (video examples) 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