• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

imeCompositionmode 0 Answers

Can't call GetKey inside OnTriggerEnter? 2 Answers

Is there something wrong with my 'Jump' input axis? 1 Answer

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

Help! Hiding/showing GUITextures! 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges