Gravity Switch by button?

Well i’m trying to make the gravity switch by press the “e” key. I had it working but players could simply spam the “e” button and fly. What can I do?

   function Update(){
        if (Input.GetKeyDown("e")) // 0 - left button; 1 - right button; 2 - middle button
        
            ThirdPersonController.gravity = -20;
}
function LateUpdate(){
        if (Input.GetKeyUp("e")); // 0 - left button; 1 - right button; 2 - middle button
        
            ThirdPersonController.gravity = 20;
            
}

You can make a timer to avoid player to push the button. Then once the player presses the button it must wait, let’s say 5 seconds before pressing the button again.

You can simply create a timer inside your script and add a condition to your if statement.

Here is an example:

// This is very simple pseudo code to illustrate what I mean
var timer : float = 0.0f; // This is the timer
var delay : float = 5.0f; // This is the delay in seconds
function Update() // or FixedUpdate()
{
    if (Input.GetKeyDown("e") && timer >= delay)
    {
        ThirdPersonController.gravity = 20 * (-1);
        timer = 0.0f
    }
    timer += Time.deltaTime;
}