Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

130
Vistas
Unity only allow shoot when visible

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?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda