How to temporarily reset player input keys?

Right now I am just testing some conditions to see if I can get it to work and it does partially.

If my player velocity.x or velocity.z >= 1.0f and the raycast does not touch the ground, that means that my player is slightly moving despite not being grounded, so I “reset” the W/A/S/D + space bar input keys to stop the movement. It works but once my player falls back down to the ground or stops moving and stands idle, I cannot move until I release and then re-press the key(s).

I am trying to change the reset so that it is temporary. So if those conditions above are met, my input keys are all reset and then my player stops moving (whether that means they fall, go from run->idle, etc), but after a certain amount of time such as 0.5f, even if my keys are still being held down the entire time, the player will start moving again since the 0.5f delay/timer is up.

I am using a character controller with default controller code, and after my controller.Move(velocity * Time) line I add CheckForMinimalMovement(move); underneath.

private bool inputResetFlag = false;
private int framesToIgnoreInput = 5;
private int framesSinceReset = 0;

void Update()
{
    if (!inputResetFlag)
    {
        ProcessInput();
    }
    else
    {
        framesSinceReset++;
        if (framesSinceReset >= framesToIgnoreInput)
        {
            framesSinceReset = 0;
            inputResetFlag = false;
        }
    }
    ...
    controller.Move(velocity * Time.deltaTime);
    CheckForMinimalMovement(move);
}

void ProcessInput()
{
    CheckKeyCombo(KeyCode.A); // check hte keys that are being pressed, not sure of a better way
    CheckKeyCombo(KeyCode.B);
    CheckKeyCombo(KeyCode.C);
    CheckKeyCombo(KeyCode.D);
}

void CheckKeyCombo(KeyCode key)
{
    bool keyPressed = Input.GetKey(key);
    bool spacePressed = Input.GetKey(KeyCode.Space);
}

void CheckForMinimalMovement(Vector3 velocity)
{
    if (velocity != Vector3.zero)
    {
        if (Mathf.Abs(velocity.x) >= 1.0f || Mathf.Abs(velocity.z) >= 1.0f)
        {
            RaycastHit downHit;
            if (Physics.Raycast(transform.position, -transform.up, out downHit, 1.1f))
            {
            }
            else
            {
                resetCounter++; // just to see explicitly how many times the key input reset is working
                Input.ResetInputAxes();
                ResetPlayerInput();
                return; 
            }
        }
    }
}

Just start a timer when you do the reset, which you increment in Update. Once you pass the timer, turn the input back on.

To ignore input, just flip a bool and guard all input processing with said flag.

Though don’t count frames. You want to be writing code that is frame rate independent.

Yeah that code was just temporary so that I could see for certain that the keys were being reset.

I did try a timer twice actually, but both times they made the code not work, without errors and debugging seemed to not give much info.

private bool inputResetFlag = false; // use flag to ignore input 
private float resetTimer = 0f;

if (inputResetFlag)
    {
        resetTimer -= Time.deltaTime;
        if (resetTimer <= 0f)
        {
            inputResetFlag = false; // Reactivate input after the timer ends
        }
    }
    else
    {
        ProcessInput();
    }

Then inside of the else statement for the raycast where the return is, I add this above it: inputResetFlag = true; but it doesn’t seem to do anything.

So when I test this, the debug console shows the reset counter is going up, so it’s working, but upon landing for example, my player is frozen on the ground despite me having lets say w+d both held down, I can only move horizontally (a-d) but no forward or backward movement. Even when the timer is supposed to end, I still need to release the keys and press them again to be able to move. I think I handled the timer correctly though

I mean if you haven’t found the issue you haven’t finished debugging.

This is the kind of thing where it helps to use the managed debugger and be able to step through your code, line by line, and see what the actual values are: Unity - Manual: Debug C# code in Unity

Obviously I can’t debug your code for you, but the concept of a using a simple timer to redirect your logic flow is something that does work and is used regularly.