How do i stop my character from over using the jump button?

I have a script I made and if I continuously spam the jump key my character starts flying. I was wondering if there’s a way to limit the script to only be able to double jump. I’m new to scripting so I’m still getting used to it all.

Script below:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class JumpingScript : MonoBehaviour
{
Rigidbody rb;
public float forcePower;
void Start()
{
rb = GetComponent();
}

// Update is called once per frame
void Update()
{

if (Input.GetKeyDown(KeyCode.W))
{
rb.AddForce(transform.forward * forcePower);
}

}
}

Hey and welcome.
The general idea is to check whether or not the player is grounded. There are different ways to do this, one would be downward raycasts. Then you’d introduce a variable containing the number of jumps the character is supposed to be able to do, and when jumping and not being grounded, reduce that number by one. Only allow jumping while that number is above 0 and return it to its default value when grounded again.

Only take the above as a general overview, as i would recommend you to look a tutorial up. There are hundreds of tutorials on movement and jumping, or more specifically double jumping.

i’ve changed it so i have double jump but I can only call on it 2 times and I’m not certain why?

Can someone point whats wrong and what I need to add to fix it?

public class JumpingScript : MonoBehaviour
{
Rigidbody rb;
public float forcePower;
public int jumps;
private IEnumerator coroutine;

void Start()
{
rb = GetComponent();
coroutine = JumpReset(2f);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.W) && jumps < 2)
{
rb.AddForce(transform.forward * forcePower);
jumps += 1;
if (jumps == 2)
{
StartCoroutine(coroutine);
}
}
}
private IEnumerator JumpReset(float waitTime)
{
yield return new WaitForSeconds(waitTime);
jumps = 0;

}
}

public class JumpingScript : MonoBehaviour
{
Rigidbody rb;
float forcePower = 0;
int jumps = 0;
float jumpTimer = 0;

void Start()
{
//reference RigidBody
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
//if we have already double jumped then subtract from timer
if(jumpTimer > 0)
{
  jumpTimer -= Time.deltaTime;
//reset jumps
if(jumpTimer<=0) jumps = 0; 
}
//else check key input
 else if (Input.GetKeyDown(KeyCode.W) && jumps < 2)
{
rb.AddForce(transform.forward * forcePower);
//increment jumps
jumps++;
//already jumped twice 
if (jumps > 1)
{
//set timer
  jumpTimer = 1;
}
}