Line that can attack an enemy

Hello,
I wanna ask about smth in c#, am doing a mini project to try new things for me and am trying to do abilities for my player to attack. For now am using the physics2d overlap smth and then a loop then an attack. What i wanna ask about is, how can i make an overlap line and do the same or if there is another way to do that pls tell me am still new here and wanna know more about programming and thanks!
and here is the code:

public class attacking : MonoBehaviour
{

    private float timeBtwAttack;
    public float startTimeBtwAttack;

    public Transform attackPos;
    public Transform zCenter;
    public Transform zPos;
    public Transform zPos1;
    public Transform xPos;
    public Transform xPos1;
    public LayerMask whatIsEnemy;
    public float attackRange;
    public Vector2 zRange = new Vector2 (3, 2);
    public float zRange1;
    public int damage;
    public int damage1;

    public GameObject forAttack;
    public GameObject forAttack1;
    public GameObject forAttack2;
    public GameObject forAttack3;
    public GameObject forAttack4;
    public GameObject forAttack5;
    public GameObject enemy;


    // Update is called once per frame
    void Update()
    {
        if (timeBtwAttack <= 0)
        {

            StartCoroutine(Zcooldown());

            if (Input.GetKeyDown(KeyCode.Mouse0))
            {
                Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsEnemy);
                for(int i = 0; i < enemiesToDamage.Length; i++)
                {
                    enemiesToDamage[i].GetComponent<enemycontrol>().TakeDamage(damage);
                }

                Debug.Log("thumbs_up");
            }

            

            if (Input.GetKey(KeyCode.A))
            {
                attackPos.position = forAttack.transform.position;
                zPos.position = forAttack.transform.position;
                zPos1.position = forAttack2.transform.position;
                zCenter.position = forAttack4.transform.position;
            }

            if (Input.GetKey(KeyCode.D))
            {
                attackPos.position = forAttack1.transform.position;
                zPos.position = forAttack1.transform.position;
                zPos1.position = forAttack3.transform.position;
                zCenter.position= forAttack5.transform.position;
            }

            timeBtwAttack = startTimeBtwAttack;
        } else
        {
            timeBtwAttack -= Time.deltaTime;
        }
    }

    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(attackPos.position, attackRange);

        Gizmos.color = Color.green;
        Gizmos.DrawWireCube(zCenter.position, zRange);

    }

    private void ZAbility()
    {
        if (Input.GetKeyDown(KeyCode.Z))
        {
            Collider2D[] enemiesToDamage1 = Physics2D.OverlapAreaAll(zPos.position, zPos1.position, whatIsEnemy);
            for (int i = 0; i < enemiesToDamage1.Length; i++)
            {
                Debug.Log("thumbs_up1");
                enemiesToDamage1[i].GetComponent<enemycontrol>().TakeDamage(damage1);
            }


        }
    }


    private void XAbility()
    {
        if (Input.GetKeyDown(KeyCode.X))
        {
            Collider2D[] enemiesToDamage2 = Physics2D
            
        }
    }


    private IEnumerator Zcooldown()
    {
        yield return new WaitForSeconds(5f);
        ZAbility();
    }

}

Note: am having a problem in the XAbility void there i wanna put the line thing.

  • sorry for putting the whole script :sweat_smile:.

Use Raycast2D. - Sorry for short answer but you will find out :slight_smile: (I am at work :smiley: )

Off topic:
You can make an array for: public GameObject forAttack;
Like: public GameObject[] attacks; ( and fill them in the Inspector )
So it take one line instead of 6 and maybe 20 in the future :smiley: