Player Only Attacks to the Right

My player can attack to the right just fine, but I don’t know how to code the attack to flip when the player goes to attack to the left. I’m sure this is something easy that can take someone 2 seconds instead of me taking 2 weeks. Appreciate any help! My first post.

Here’s the Combat script:

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

public class PlayerCombat : MonoBehaviour
{

public Animator animator;
 
  public Transform attackPoint;
  public LayerMask enemyLayers;
  public float attackRange = 0.5f;

public int attackDamage = 40;

    void Update()
    {
      if (Input.GetKeyDown(KeyCode.X))
      {
Attack();
      }
    }
    void Attack()
    {
//Play an attack animation
animator.SetTrigger("Attack");

//Detect enemies in range of attack
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);

//Damage them
foreach(Collider2D enemy in hitEnemies)
{
    enemy.GetComponent<Enemy>().takeDamage(attackDamage);
}
    }

void OnDrawGizmosSelected()
{
if(attackPoint == null)
return;

  Gizmos.DrawWireSphere(attackPoint.position, attackRange);
}
}

And here’s the Player Movement script if it’s helpful:

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

public class PlayerMovement : MonoBehaviour
{
     private Rigidbody2D rb;
     private BoxCollider2D coll;
     private SpriteRenderer sprite;
     private Animator anim;

[SerializeField] private LayerMask jumpableGround;

     private float dirX = 0f;
     [SerializeField] private float moveSpeed = 7f;
     [SerializeField] private float jumpForce = 14f;

     private enum MovementState { idle, running, jumping, falling }

    // Start is called before the first frame update
    private void Start()
    {
      rb = GetComponent<Rigidbody2D>();
      coll = GetComponent<BoxCollider2D>();
      sprite = GetComponent<SpriteRenderer>();
      anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    private void Update()
    {
dirX = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);

      if (Input.GetButtonDown("Jump") && IsGrounded())
      {
          rb.velocity = new Vector2(rb.velocity.x, jumpForce);
      }

UpdateAnimationState();
    }
  
    private void UpdateAnimationState()
    {

MovementState state;

     if (dirX > 0f)
      {
         state = MovementState.running;
          sprite.flipX = false;
      }
      else if (dirX < 0f)
      {
         state = MovementState.running;
          sprite.flipX = true;
      }
      else
      {
        state = MovementState.idle;
      }

if(rb.velocity.y > .1f)
{
    state = MovementState.jumping;
}

else if (rb.velocity.y < -.1f)
{
    state = MovementState.falling;
}
      anim.SetInteger("state", (int)state);
    }

private bool IsGrounded()
{
    return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
}
}

I’m guessing attackPoint is an empty GameObject that’s attached to the right side of your character? I think you should change it to a Vector3, rather than a GameObject, and flip the X value when your character is facing left when you attack.

Thank for the response. I just ended up finding a tutorial and restructuring the entire movement code and after a few hours it works. I’ll keep this in mind for the future though