Input.GetKeyDown() and Input.GetKeyUp() dosent work every every time
Sobasicly when I run the code when I press 'w' it doesn't always detect it and it doesn't print down and when I realise 'w' it doesn't print up so I'm a bit confused. Don't know if it's my code or just something wear with my keyboard so please help me. This my code:
Answer by streeetwalker · Apr 16, 2020 at 06:31 PM
@Kisielekw, Your code looks fine, unless grounded is not what you expect, so grounded may be the issue. Debug grounded before the if statement.
Just a personal style note - this is not the problem - but for consistency I use the Double Ampersand && for a Conditional AND. Its fine with a single & with booleans, but with anything else it is a Logical AND operation
// in the case of booleans && and & are the same
// but as a matter of style
if( Input.GetKeyDown("w") && grounded )
// don't need to test for grounded == true because grounded is a boolean.
I don't think it the grounded variable as I set as a public one so I can see what grounded is and when the character (the blue one) is on the ground it indicates that it's set to true.
Well, probably not then, but debug grounded just to be sure.
Otherwise, the problem may be your keyboard, so you need to follow through with different tests. Try testing a different key to see if is the W key alone, or the problem manifests itself with all keys. You'll have to try another computer system or external keyboard. Try with $$anonymous$$ouseButtonDown to see if behaves the same way.
Where are you doing your key test, in an Update loop or what?
I was doing the key test in a fixed update loop is that incorrect? I'm just getting started with unity so I'm playing around a bit seeing what works and what doesn't.