How to have a speed Boost in my game?

I have my 2d game. The player can move and then a button is pressed they have a speed boost. How can I make it where the speed boost only works for 2 seconds and make the player wait 5 seconds before using it again? Also how would I add a bar to show the player when they can use it again? I’m using c#

Coroutines work well for that purpose, so you might be looking for something along these lines:

public class BoostExample : MonoBehaviour
{
    public float boostCooldown = 5f;
    public float boostDuration = 2f;
    private float speedBoost = 3;

    private bool hasCooldown;
    private Vector3 normalMovementVector = Vector3.forward;
    private Vector3 currentMovementVector;

    void Start()
    {
        currentMovementVector = normalMovementVector;

        // doesn't allow to have speed right at the beginning
        // but comment it out if you want to have boost immediately at startup
        StartCoroutine(ActivateCooldown());
    }
    
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space) && !hasCooldown)
        {
            // apply boost, i simply added another vector to it
            currentMovementVector += Vector3.forward * speedBoost; 
            // activate the cooldown and start the deactivation method for the boost
            StartCoroutine(ActivateCooldown());
            StartCoroutine(ResetMovementVector());
        }
        // just some basic movement for the test
        transform.Translate(currentMovementVector * Time.deltaTime);
    }

    IEnumerator ResetMovementVector()
    {
        // wait some seconds
        yield return new WaitForSeconds(boostDuration);
        // return to normal speed
        currentMovementVector = normalMovementVector;
        Debug.Log("boost ended");
    }

    IEnumerator ActivateCooldown()
    {
        // put some code to disable the boost-is-ready bar
        // diable the ability to use boost
        hasCooldown = true;
        // wait until the boost is ready again
        yield return new WaitForSeconds(boostCooldown);
        // make the boost usable
        hasCooldown = false;
        Debug.Log("boost ready"); 
        // put some code to enable the boost-is-ready bar
    }
}

I’n not very good at articulating answers so i wrote a small script to show how it could be done, there are probably better ways, the variables are public so that you can see them in the editor.

using UnityEngine;
using System.Collections;

public class boost : MonoBehaviour {
	public float boostTime = 2.0f;
	public float currentBoostTime;
	public float boostDelayTime = 5.0f;
	public float currentBoostDelayTime;
	public bool boosting = false;
	public float time;

	public float baseSpeed = 1.0f;
	public float speedBoost = 2.0f;
	public float speed;

	// Use this for initialization
	void Start () {
		currentBoostTime = 0f;
		currentBoostDelayTime = 0f;
		speed = baseSpeed;
	}

	void movePlayer()
	{
		if (Input.GetKeyDown(KeyCode.Q) && !boosting && Time.time > currentBoostDelayTime) { //or whatever your boost button is
			currentBoostTime = Time.time + boostTime; //start the timer for the boost
			boosting = true;
		}

		if ((Time.time > currentBoostTime) && boosting) { // am i boosting? has the boost timer expired?
			boosting = false;
			currentBoostDelayTime = Time.time + boostDelayTime;
		}

		if (boosting) {
			speed = speedBoost;

		}
			
		if (!boosting) {
			speed = baseSpeed;

		}
	}


	// Update is called once per frame
	void Update () {
		time = Time.time; //debug
		movePlayer ();
	}
}