- Home /
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?
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.
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!)
@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 :)
You are right. It will do everything in Update in one frame. 3 steps forward, 2 back = 1 forward per frame.
Your answer

Follow this Question
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