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
-
Any comments on WHY this is?
-
Are there any alternatives should I have to make a command that requires the player to NOT hold down a key/button?