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 James Wassall · Nov 24, 2013 at 12:43 PM · c#if-statementsifinput.getkey

Input.GetKey(KeyCode.E) requires multiple presses.

Hello all,

I am trying to make a variable based script for my vehicular based game. As i'm making a simulator I need/would like a Engine variable for when the engine is either "on" or "off". To do this I have used:

 public float engine;

and

 void Start () {
      engine = 0;
 }

To control the variable. For the button input I am using:

 if(Input.GetKey(KeyCode.E)){
        if(engine == 0)
             engine = 1;
        else
             engine = 0;
 }

Yet this method only works the first time "E" is pressed then it requires two quick presses if not more. Is there a better solution which works all the time?

Many Thanks,

~James W

Comment
Add comment · Show 3
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 ozturkcompany · Nov 24, 2013 at 12:46 PM 0
Share

you need to use getkeydown, geykey will only work for the time you keep pressing.

avatar image James Wassall · Nov 24, 2013 at 01:32 PM 0
Share

Thanks for the fast reply @ozturkcompany your solution worked but tanoshimi wrote that as an answer which I could accept.

avatar image ozturkcompany · Nov 24, 2013 at 01:35 PM 0
Share

hi no problem at all :) cheers

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by tanoshimi · Nov 24, 2013 at 01:10 PM

Assuming that you're using Input.GetKey inside Update(), your current code is flipping your engine on and off in every frame that the E key is pressed - possibly 60 times a second or more. Without realising, you're actually toggling the engine variable many times in every touch at the moment, and it's probably pretty arbitrary whether it ends on 0 or 1.

What you probably want is to use Input.GetKeyDown, which only returns true for the first frame in which a key is held.

Comment
Add comment · Show 1 · 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 James Wassall · Nov 24, 2013 at 01:30 PM 0
Share

Thanks for the detailed explanation as to why this was happening. Using Input.Get$$anonymous$$eyDown fixed the issue and it is now a toggle.

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

18 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

Related Questions

Make script affect only one GameObject instead of making all object with this script be affected. 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

play an audio clip in an if statement 2 Answers

Use multiple values for "if" statement 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