Well this is my Enemy Script:
public class Enemy : MonoBehaviour
{
public GameObject explosion;
public float speed;
public float shotDelay;
public GameObject enemyBomb;
public bool canShot;
public int pointValue = 30;
IEnumerator Start()
{
GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
if (canShot == false) yield break;
while (true)
{
for (int i = 0; i < transform.childCount; i++)
{
Instantiate(enemyBomb, transform.position, transform.rotation);
}
yield return new WaitForSeconds(shotDelay);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
FindObjectOfType<Score>().AddPoint(pointValue);
Instantiate(explosion, transform.position, transform.rotation);
Destroy(gameObject);
}
private void OnBecameInvisible()
{
Destroy(gameObject);
}
}
The problem is, that the enemy shoots before he is visible so there is no chance to fight him. Is there a way how to check if the Enemy is visible or not?
maybe you should try to add a trigger - so when the enemy touches the trigger that's set around the player vision something (let's say isvisible = true) should be set to true and every time the enemy wants to shoot it checks if (isvisible == true){//shoot}. And when the enemy goes out of the trigger OnTriggerExit2D(Collider2D collision) it sets isvisible = false
I hope it will work for you.