Решил Проблему, вот мой код:
void Update()
{
transform.Translate(Vector2.right * Time.deltaTime * speed);
DamageObject();
}
void DamageObject()
{
Vector2 mousePosition = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
Vector2 firePointPosition = new Vector2(Gun.gun.firePoint.position.x, Gun.gun.firePoint.position.y);
RaycastHit2D raycastHit2D = Physics2D.Raycast(firePointPosition, mousePosition - firePointPosition, 100, toHit);
Vector2 dir = raycastHit2D.point - (Vector2)transform.position;
float dist = speed * Time.deltaTime;
if (rangeRadius > 0)
{
Collider2D[] collider2D = Physics2D.OverlapCircleAll(transform.position, rangeRadius);
foreach (Collider2D col in collider2D)
{
if (col.tag == enemyName)
{
DamageEnemy(col.transform);
Destroy(gameObject);
}
}
}
else if (dir.magnitude <= dist)
{
if (raycastHit2D.collider != null)
{
Enemy enemy = raycastHit2D.collider.GetComponent<Enemy>();
if (enemy != null)
{
enemy.TakeDamage(damage);
Destroy(gameObject);
}
}
}
}
void DamageEnemy(Transform enemy)
{
Enemy e = enemy.GetComponent<Enemy>();
if (e != null)
{
e.TakeDamage(damage);
}
}