C#/.net Run function on first frame of key being pressed

Viewed 22

I need to run a function only on the first frame of a key being pressed. Something like the unity GetKeyDown.

1 Answers

define a bool to track if the key has been previously pressed like this:

bool HasKeyBeenPressed=false;

Then when your key gets pressed, check that HasKeyBeenPressed==false before you run the code (once) and set the flag to true, like this.

if (Input.GetKey(KeyCode.A))  // A key pressed
{
    if (HasKeyBeenPressed==false) 
    {
        // do something Once here
        HasKeyBeenPressed=true;
    }

    // Handle normal keypress for A here
}
Related