Question by
MrBrillsbury · Dec 30, 2017 at 08:12 AM ·
cameraspawningvisibilityenemiesvisible
How can I stop enemies spawning when the spawner is visible by the camera?
public GameObject enemy;
public float timeSinceLastSpawn;
public float spawnTime = 5f;
public Transform[] spawnPoints;
public int spawnCount;
public int spawnLimit;
public bool spawnability = true;
void Start () {
InvokeRepeating("SpawnTime", spawnTime, spawnTime);
}
void Update () {
if (spawnability = true)
{
timeSinceLastSpawn += Time.deltaTime;
if (timeSinceLastSpawn > spawnTime)
{
if (spawnCount < spawnLimit)
{
int spawnPointIndex = Random.Range(0, spawnPoints.Length);
Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
spawnCount++;
timeSinceLastSpawn = 0;
}
else
{
spawnability = false;
}
}
}
}
What can I do to this code so that the enemies don't spawn if the spawnPoints is visible by the camera?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612145503im_/https://answers.unity.com/themes/thub/images/avi.jpg)