I’ve been trying to make a grid movement system, it’s a simple structure of:
If getkey and not moving, run coroutine which flips moving on, executes the Lerp to the next grid square, flips moving off so the next coroutine can run. This seems to work smoothly but every now and then moving around unity just thinks the key is held down. I’ve re-written the code as a coroutine, as a update loop checking if the target position and yours match, but in every instance unity randomly doesn’t register the get key up and keeps me spinning or running in a straight line. Sometimes I tap once to move and hit a wall and get the collision register twice as if I’ve tapped the key twice or held it down.
I’ve tried to search the forums, look for similar experiences but found nothing. The code is super simple, but I can’t do proper movement code if the program keeps thinking I’m holding the key down. (note: I want to support holding it down for continuous movement as well as single tap for moving 1 grid square, but this bug is throwing off all my tests).
I’ve tried Unity 2020 and 2021, both seem to give me the issue. I’m on a Mac and tried with both wired and bluetooth keyboard. My scene is simple with a few pro builder blocks and a plane, nothing that might cause hitching. I’m at a loss! Please help.
With or without the collision check (just a raycast in the direction I intend to move looking for colliders). It still functions the same, moving me randomly sometimes just double sometimes continuously. It seems to respect the timer on the coroutine, I don’t move unless it’s completed the lerp, but it still thinks I have the w key pressed even when I’ve released it and keeps moving forward indefinitely unless I hit another key.
Well, your code you shared doesn’t use GetKeyUp/GetKeyDown.
It only uses GetKey, and GetKey returns true if the key is in an activated state (down or held).
Now I’m not 100% on what you’re attempting to do… but that’s regardless of your original statement which is claiming “Unity randomly doesn’t register GetKeyUp”. You don’t use GetKeyUp, so that can’t be the problem.
Furthermore you don’t actually show where Input.GetKey is getting called from (is this in Update? FixedUpdate? yet another Coroutine?) because the context of that if statement will impact its behaviour further.
I apologize, it is in the update function. I am registering when the key is held each frame to attempt to call the coroutine. Since when the coroutine is running the bool is flipped to true, the if statement in update() never calls the coroutine till the last one is complete.
While I am not specifically looking for get key up or get key down, I expected unity to register when the key is no longer being held down each frame and thus would not continue to run the coroutine in the if statement even if no keys on the keyboard were held down.
I do want the ability to run the coroutine every duration of timeToMove by holding down the w key, but 50% of the time unity registers that my finger has lifted and when the movement is complete doesn’t attempt to move again. Sometimes it does two movements when I just tap the key (which I thought would not be possible given that the bool is flipped so the coroutine should not be attempted twice). And sometimes it just keeps running it over and over as if the key is held down.
Indeed thank you, it is in the update loop so I assumed it would be checking if the key was held down each frame, however it just randomly continues to run once I release the key sometimes and will continue to think the key is pressed until I strike another key.
Here is the code, as you can see it’s pretty simple. I just don’t know why I get these issues with unity. I’ve tried closing all other apps, using a wired keyboard, but it keeps happening.
Try simplifying the question: Is Unity reporting an incorrect key state, or is your code reacting incorrectly to correct key input? Try this:
public bool up = false;
public bool down = false;
public bool left = false;
public bool right = false;
//insert at beginning of Update
up = Input.GetKey(KeyCode.W);
//etc
Now, select this object while the game is running and watch these four checkboxes in the inspector. When you trigger the issue, see whether the expected checkbox remains checked. If the checkboxes don’t 100% match your keyboard input, then the problem is happening at the hardware/OS/Unity level somewhere. If the checkboxes match but the issue is happening, then the problem is something in the way your code responds to the buttons presses.