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 Arcterran · Aug 19, 2013 at 02:41 AM · inputif-statements

Is there a reason GetKeyDown doesn't work with this code but GetKey does?

So I'm writing code that would control whether or not a player is swimming, climbing a ladder, ect. What the state change would do is in essence change the control scheme of the player, example: if 'playerState' is "normal" gives you your typical platformer controls, walk left, walk right, jump ect. However if the 'playerState' is "climbing" restricts you to moving only up and down, because you would be climbing on a ladder

Anyway here is the relevant code:

     public string playerState = "Normal";
 
     void Update () {
         bool contextHit = Input.GetKeyDown(KeyCode.F);
         bool contextHold = Input.GetKey(KeyCode.F);

     if(contextHit == true) {
             print ("F key Hit!");
             playerState = "climbing";
         } 
 
         if (contextHold == true) {
             print("F key Held!");
             playerState = "climbing";
         } 
 
 }

So here's the problem, I initially had the first if statement (contextHit) as my code and the state was NOT changing, however it was reading the fact that the F key is hit. The control scheme or 'playerState' did not change, but "F key Hit!" was being written to console.

However when I commented it out and used 'contextHold' not only did it register the key hit and write to console "F key Held!" but the 'playerState' changed to "climbing" and the controls changed appropriately.

So my question is twofold

1) Any comments on WHY this is?

2) Are there any alternatives should I have to make a command that requires the player to NOT hold down a key/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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Peter G · Aug 19, 2013 at 03:36 AM

My first guess is that you are overwriting it somewhere later down the function or something along those lines. I would see if the playerState becomes "climbing" during the frame its pressed.

So either print() the playerState immediately after setting it (like the next line), or set a breakpoint there and then run it using a Debugger. If it is the correct state during that frame, then your problem is somewhere else.

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 Arcterran · Aug 19, 2013 at 03:58 AM 0
Share

Good call! it was this snippet(I commented it out to test)

         if (contextHit == true && playerState == "climbing") {
             playerState = "normal";
         }

I think what happened was that I thought each frame = one line for some stupid reason, but then I remember that update was called once per FRA$$anonymous$$E Like if update always told a cube to "take three steps left and two steps back" the cube ins$$anonymous$$d of taking three steps left in the first frame then two steps back the next, It takes them BOTH at once.

at least I think I'm getting this right! (If I'm not by all means correct me!)

avatar image Benproductions1 · Aug 19, 2013 at 07:54 AM 0
Share

@Arcterrain Going by your examples, yes everything in Update gets done before the frame is rendered, however if you told it to go 3 steps left and two steps back, it would first go 3 steps back and then go 2 steps back.

Always remember that a computer (more specifically a CPU) can never do two things at once, it can only do many things quickly in succession :)

avatar image DaveA · Aug 19, 2013 at 07:59 AM 0
Share

You are right. It will do everything in Update in one frame. 3 steps forward, 2 back = 1 forward per frame.

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

New Input system holding down a button 2 Answers

Input.GetMouseButtonDown(0) running through my if statments too quickly 1 Answer

Weird Input System situation 1 Answer

Branching Text help. 1 Answer

if statement error 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