I tried many times, searched on different sites … Nothing works. The problem is in the operator if(hit.collider.gameObject == target).
I want to make the turret shoot only when the enemy is in front of it.
i changed configurations method Raycast()
Vector2 up = transform.TransformDirection(Vector2.right) * 5;
Debug.DrawRay(transform.position, up, Color.green, 1);
if (!shoot)
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, up, 0.1F);
if (hit != null)
if (hit.collider.gameObject == target)
{
Instantiate(Bullet, shootElem.position, transform.rotation);
shoot = true;
Invoke("reload", shootSpeed);
}
}
}
}
void reload()
{ shoot = false; }