I have programmed a enemies spawner that spawns enemies in a random circle around the player. But the enemies keep spawning on other gameobjects like houses. Is there anyway to evoide that?
And here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
[SerializeField]
public float spawnRadius = 7, time = 2.50f;
public int Enemysnow = 0;
public int enemyMax = 35;
private bool maxreached = false; // Bool that stores the Information if the maximum is reached
public GameObject[] enemies; //List of enemies that can get spawned
// Start is called before the first frame update
void Start()
{
StartCoroutine(SpawnEnemy()); //Start the Couroutine once at the start of the game
}
IEnumerator SpawnEnemy() // that is the important part
{
while (true)
{
if (!maxreached)
{
Vector2 spawnPos = GameObject.Find("Player").transform.position; //Spawning an enemy
spawnPos += Random.insideUnitCircle.normalized * spawnRadius;
Instantiate(enemies[Random.Range(0, enemies.Length)], spawnPos, Quaternion.identity);
yield return new WaitForSeconds(time);
Enemysnow++; // increase the enemy counter
}
yield return null;
}
}
public void lowerMaxEnemys()
{
Enemysnow--; // decrease the enemy counter if a enemy is killed(Kill is in another script)
}
public void Update()
{
maxreached = Enemysnow >= enemyMax;
}
public void HigherMaxEnemys(int amount)
{
enemyMax += amount;
}
public void HigherSpawnRade(float amount)
{
time -= amount;
}
}