I’m making a really simple 3D game right now and I’m trying to make it so that enemies will spawn in 3 different regions in my game. My code right now only spawns enemies in a small range but they get stuck in some places where I don’t want them to spawn. How can I fix this issue so that I make them spawn in three specific regions of my map?
Here is my code for my spawn manager so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
[SerializeField] private GameObject enemyPrefab;
[SerializeField] private float enemySpawnInterval = 3.5f;
public GameObject[ ] spawnLocations;
// Start is called before the first frame update
void Start()
{
StartCoroutine(spawnEnemy(enemySpawnInterval, enemyPrefab));
}
private IEnumerator spawnEnemy(float interval, GameObject enemy)
{
yield return new WaitForSeconds(interval);
GameObject newEnemy = Instantiate(enemy, new Vector3(Random.Range(-5f, 5), 0.5f, Random.Range(-5f, 5f)), Quaternion.identity);
StartCoroutine(spawnEnemy(interval, enemy));
}
}