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?
question from:
https://stackoverflow.com/questions/65920630/unity-only-allow-shoot-when-visible 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…