How would i go about having my character only dash right, when hes facing right and dash left when hes facing left?

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

public class Dash : MonoBehaviour
{

public Rigidbody2D rb;
public float dashSpeed;
private float dashTime;
public float startDashTime;
private int direction;
private bool isDashing;
[SerializeField]
private GameObject dashEffect;

// Start is called before the first frame update
void Start()
{
    
    rb = gameObject.GetComponent<Rigidbody2D>();
    dashTime = startDashTime;

}

// Update is called once per frame
void Update()
{
    if (direction == 0)
    {
        if (Input.GetKeyDown(KeyCode.R ))
        {
            
            Instantiate(dashEffect, transform.position, Quaternion.identity);
            direction = 1;
            isDashing = true;
        }
        else if (Input.GetKeyDown(KeyCode.T))
        {
            
            Instantiate(dashEffect, transform.position, Quaternion.identity);
            direction = 2;
            isDashing = true;
        }
    }
    else
    {
        if (dashTime <= 0)
        {
            direction = 0;
            dashTime = startDashTime;
            rb.velocity = Vector2.zero;
            isDashing = false;
        }
        else
        {
            dashTime -= Time.deltaTime;

            if (direction == 1)
            {
                transform.localScale = new Vector3(-1, 1, 1);
                rb.velocity = Vector2.left * dashSpeed;
            }
            else if (direction == 2)
            {
                transform.localScale = new Vector3(1, 1, 1);
                rb.velocity = Vector2.right * dashSpeed;
            }
        }
    }
}  

}

If you use a variable to keep track of the direction you are facing it becomes a very simple implementation. The script below is a variant on the one provide with some major code changes but essentially same functionality.

int facingDir;
bool dashOnCooldown;
float dashCooldownTime;
public Rigidbody2D rb;
public float dashSpeed;
 
void Start () { 
	facingDir = 0;
	rb = gameObject.GetComponent<Rigidbody2D>();
}

void Update () { 

	// Handle facing direction
	HandleDirection();
	
	// Handle movement
	HandleMovement();
	
}

void HandleDirection () {
	// Left facing
	if (Input.GetKeyDown (KeyCode.A))
		facingDir = -1;
	
	// Right facing
	if (Input.GetKeyDown (KeyCode.D))
		facingDir = 1;
}

void HandleMovement () {
	
	// Dash
	if (!dashOnCooldown){
		rb.AddForce(new Vector2(facingDir, 0) * dashSpeed, ForceMode.Impulse);
		StartCoroutine(DashCoolDown ());
	}
	
	// Other Movement
	// ...
}

IEnumerator DashCoolDown () {
	dashOnCooldown = true;
	yield return new WaitForSeconds (dashCooldownTime);
	dashOnCooldown = false;
}

Thank you for your response i appreciate it. I’m almost there, the player now faces right when i dash right and faces left when i dash left.

Having said that, I’ve now encountered another issue. When attempting to move left or right after dashing in the opposite direction, the player will face right when i move left and face left when i move right.

The code has been updated. can anyone help? Thanks.