Trying to cancel a KeyDown Button without releasing it

I am attempting to cancel the first if at signs of death or at thruster <= 0 but every combination of the if statement seems to keep breaking the speed variable and the thruster sprite doesn’t shut off on death or when thruster bar hits 0.

if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            gameObject.GetComponent<SpriteRenderer>().enabled = true;
            player.IncreaseCurrentSpeed(_increaseSpeed);
            _thrustersState = true;
        }
        else if (Input.GetKeyUp(KeyCode.LeftShift))
        {
            gameObject.GetComponent<SpriteRenderer>().enabled = false;
            player.DecreaseCurrentSpeed(_increaseSpeed);
            _thrustersState = false;
        }

Attempts
playerState() = bool isDead

  • gave first if && player.State() != true && thrusterGuage.GetIndex() > 0
  • was going to add || additions to second if statement but realized that would decrease the speed variable ever frame so canceled that line of thought.

Sounds like you need to be evaluating the else if and not the first. From what I can gather, you want to cancel the thrust if the player dies or the thruster charge is depleted. GetKeyDown and Up only evaluate true for the frame in which the defined button is pressed or released, so don’t add to them rather also run the code if other factors are true.

This should do it, although I would imagine it could be cleaned up a bit. Hard to say without seeing more code.

if (Input.GetKeyDown(KeyCode.LeftShift))
{
    gameObject.GetComponent<SpriteRenderer>().enabled = true;
    player.IncreaseCurrentSpeed(_increaseSpeed);
    _thrustersState = true;
}
else if (Input.GetKeyUp(KeyCode.LeftShift) || _thrustersState && (thrusterGuage.GetIndex() <= 0 || player.State() != true))
{
    gameObject.GetComponent<SpriteRenderer>().enabled = false;
    player.DecreaseCurrentSpeed(_increaseSpeed);
    _thrustersState = false;
}

Thank you very much it wasn’t quite correct but it got me in the correct direction I needed to go in to get it working. Very helpful.