How do I limit the number of times my player can jump while also using a 2 second timer cool down?

So I’m working on a little sandbox project game (Following a tutorial/class online, but I’d like to change things up and add my own flavor to it).

This is my first time trying to make a game as well as my first time coding. Complete beginner , any advice/tips would be highly appreciated

The game is like a 3d platformer, I’d like to limit the amount of jumps the player is able to perform while in mid air. (which is most of the game)

I’d like the player to be able to jump maybe twice or three times. (would prefer to serialize a value so I could maybe change it in game for both the time and amount of jumps) I’m just stuck on how to actually implement the methods.

How can I implement a 2 second cool down delay on jumping? Currently the players able to jump infinitely.

Here’s the code i have so far is C#


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

public class Movement : MonoBehaviour
{

[SerializeField] float mainThrust = 100f;
[SerializeField] float rotationThrust = 1f;
[SerializeField] AudioClip jump;
Rigidbody rb;
AudioSource audioSource; 

// Start is called before the first frame update
void Start()
{
   rb = GetComponent<Rigidbody>();
   audioSource = GetComponent<AudioSource>();
}

// Update is called once per frame
void Update()
{
    ProcessThrust();
    ProcessRotation();
}

void ProcessThrust()
{
    if (Input.GetKey(KeyCode.Space))
    {
        rb.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);
        if(!audioSource.isPlaying)
        {
            audioSource.PlayOneShot(jump);
        }
    }
    else
    {
        audioSource.Stop();

    }
}

void ProcessRotation()
{
    if (Input.GetKey(KeyCode.A))
    {
        ApplyRotation(rotationThrust);
    }
    else if (Input.GetKey(KeyCode.D))
    {
        ApplyRotation(-rotationThrust);
    }
}

void ApplyRotation(float rotationThisFrame)
{
    rb.freezeRotation = true; //freezing roation so we can manually rotate
    transform.Rotate(Vector3.forward * rotationThisFrame * Time.deltaTime);
    rb.freezeRotation = false; //unfreezing 
}

}


Thanks for your time and help.

I would suggest using a coroutine for the cool down which sets a canJump bool to false while waiting:
using System.Collections;

public class Movement : MonoBehaviour
{
bool canJump = true;

int maxJumps = 3;
int currentJump = 0;

private void Update()
{

    if (Input.GetKey(KeyCode.Space) && canJump)
    {
        //your jump code goes in here along with the below code
        currentJump++;
        
        if(currentJump == maxJumps)
        {
            StartCoroutine(CoolDown());
        }
    }
}
private IEnumerator CoolDown()
{
    currentJump = 0;
    canJump = false;
    yield return new WaitForSeconds(2);
    canJump = true;
}
}

That is just a basic example, but I hope you get the idea.

Thanks! Trying to make it work using this. I’ll post an update after i get the script to function properly.