The question is answered, right answer was accepted
GetKeyUp does not get called sometimes.
Hello Community. My problem is that sometimes the GetKeyUp does not get called!
My Code:
void Update()
{
if (Input.GetKeyDown(KeyCode.D))
{
walking = true;
}
else if (Input.GetKeyUp(KeyCode.D))
{
walking = false;
}
print (walking);
}
So sometimes the Console tells me true even if I have already stopped pressing "D".
Thank you :)
(And No I can not use GetKey instead of this 2 Calls cause I need to know when it is not pressed anymore)
Answer by NoseKills · Jun 28, 2014 at 10:52 PM
I'm not aware of a bug that would cause GetKeyUp to not trigger. Put a print in both the keyDown and keyUp to see if the Up case really doesn't get fired. Also make sure you are not setting walking
to true anywhere else.
Not sure what you mean by
(And No I can not use GetKey instead of this 2 Calls cause I need to know when it is not pressed anymore)
Shouldn't this work in any case?
var wasWalking:bool;
void Update()
{
walking = Input.GetKey(KeyCode.D);
if (wasWalking)
{
if (!walking)
{
// key was released
}
}
else
{
if (walking)
{
// key was pressed
}
}
wasWalking= walking;
print (walking);
}
This way "walking" will be true when D is pressed and false if not, and if you need to do something special the frame when it's pressed or released, you can do it in the if/else. If not, just erase the whole if/else
Thank you! Now it works. Didn't think of that! You are awesome :)