- Home /
Input.GetKey missing keyboard state on Update and FixedUpdate
I am trying to check at every FixedUpdate if the keyboard 'right' or 'left' key is pressed or not. On every FixedUpdate because that is where I am calculating and applying physics. The code is a simple:
// RIGHT
if(Input.GetKey("right"))
{
currentInput = currentInputEnum.RIGHT;
}
// LEFT
else if(Input.GetKey("left"))
{
currentInput = currentInputEnum.LEFT;
}
// NO INPUT
else
{
currentInput = currentInputEnum.NOINPUT;
}
Sometimes I stop pressing the key but it doesn't change the currentInput variable, so it keeps a wrong state. For example, sometimes I stop pressing 'right' and am not pressing any key, but the state stays at right; the variable stays as RIGHT.
I have tried placing the code on Update as well as FixedUpdate.
I have recorded the screen with mac's keyboard viewer, and this variable exposed on Unity's editor. I can see on keyboard viewer that the key was released, but the variable stays unchanged.
If I press any key, then it updates the input variable correctly, but until then it stays on the old state. Strangely, if I move my mouse it updates the variable correctly as well, even though I am not using the mouse in the game at all.
This happens about 1 in a 100 key holding situations, enough to break the game. I have tried a lot of things but am short on ideas now. I have looked elsewhere on the code to see if there is something interfering on that, but it doesn't look like it, input is managed only on this place and this variable is set only there as well.
Any ideas on this, or how to implement a FixedUpdate key holding, continuous, detection are appreciated.
just to note: You can separate the GetKey under Update and any physic under FixedUpdate in the same class.. this preferred way. Also make sure when testing inputs the application focus stays in "Game" window..
I think I have this too @lifeisabeach - did you ever figure out what's going on? What kind of Mac are you on?
Answer by Xquality · Nov 09, 2020 at 12:26 AM
Doubt this will make a difference, but try this. If it doesn't fix it, then I suggest looking into the new input system or using booleans instead of enums. With the new input system, you can set up delegates which trigger certain things. I recommend it ;)
void FixedUpdate()
{
currentInput = Input.GetKey("right") ? currentInputEnum.RIGHT : Input.GetKey("left") ? currentInputEnum.LEFT : currentInputEnum.NOINPUT;
}
Thanks for the response @Xquality, I spent the past days trying to fix this, even went so far as implementing a basic solution with the new input system, but the same problem was happening. I then made a $$anonymous$$ac build and on the build the problem doesn't seem to happen.
It is weird because Unity's Update/FixedUpdate doesn't register the key up some times, only after I move the mouse or hit another key (2 things that have nothing to do with my inputs in the game and aren't even checked in code), then it updates the state in Unity. I am wondering if it has something to do between the $$anonymous$$ac and Unity, some kind of stress situation. Any ideas are welcome since it's a game breaking bug that I can't track yet.
Answer by merctraider · Nov 09, 2020 at 07:06 AM
Maybe instead of doing else if statements you can try making them separate if statements since they aren't mutually exclusive; Players can press right and left at the same time. I tried it in fixedupdate and it works on my end.
if (Input.GetKey(KeyCode.RightArrow))
{
currentInput = InputEnum.RIGHT;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
currentInput = InputEnum.LEFT;
}
Debug.Log(currentInput);
if(!Input.GetKey(KeyCode.RightArrow) && !Input.GetKey(KeyCode.LeftArrow))
{
currentInput = InputEnum.NOINPUT;
}
Thanks for the response @merctraider, you are right about the player pressing right and left at the same time, I actually check for that in code. I have tried a lot of solutions to this problem in the past days without success. But I noticed that on a $$anonymous$$ac build the problem doesn't happen, only when running on editor.
This is a strange bug because Unity sometimes doesn't capture that the 'right' or 'left' key was released, only after I move the mouse or hit another key on the keyboard (even thought I am not using the mouse or any other key in the keyboard, and not even checking for those). So, now I am thinking it is something between the $$anonymous$$ac and Unity. But still worried because it's a bug I can't track and could happen on other situations, maybe. So, any other ideas are welcome as well.
Your answer
Follow this Question
Related Questions
Eliminating input loss 1 Answer
Is Input detection ok to do in fixedupdate? 7 Answers
Input.GetKey not working 1 Answer
Is it okay to use ForceMode.VelocityChange in Update()? 1 Answer