Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!