- Home /
How to spawn enemies on my spawnpoints
Hey so im trying to make my enemies spawn on a random spawnpoint i have but when i try it it spawn at random positions instead of the spawnpoint this is how they spawn and my spawnpoints
public GameObject[] spawnPoints;
void Start()
{
spawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint");
es = SpawnRandomEnemy();
StartCoroutine (es);
}
WaitForSeconds wait = new WaitForSeconds(spawnInterval);
while (currentNumberOfEnemies < maxNumberOfEnemies)
{
int enemyIndex = Random.Range(0, enemyPrefabs.Length);
Vector3 spawnPos = new Vector3(Random.Range(0, spawnPoints.Length), 1);
Instantiate(enemyPrefabs[enemyIndex], spawnPos, enemyPrefabs[enemyIndex].transform.rotation);
currentNumberOfEnemies++;
yield return wait;
}
Answer by valentingurkov · Mar 14, 2020 at 12:40 PM
You need to use the position of the spawnpoint in spawnPos. Maybe you can make a method to get a random spawnpoint form your spawnpoints or just try it with the transform.position of the first spawnpoint during instantiate. This should place your mobs on the spawnpoint.
You mean spawnPoints[0].transform.position does not work?
I am not sure where you want me to use spawnPoints[0].transform.position
I will try to explain: You have the line Instantiate(enemyPrefabs[enemyIndex], spawnPos, enemyPrefabs[enemyIndex].transform.rotation); and currently your objects are instatianted with the position of spawnPos. This varaible does not contain the position of one of your spawnpoints, but some random vector. As an example, you can replace spawnPos with spawnPoints[0].transform.position and it should spawn all your mobs in your first spawnpoint. Then it is a matter of replacing the hard coded [0] index with a random one, so your mobs spawn randomly across your spawnpoints. If I understood your situation correctly, then this should more or less work.
Your answer
Follow this Question
Related Questions
Randomly spawning objects,Random Object and Spawn Point? 2 Answers
Photon Spawn on server, not client 0 Answers
Game object spawning, spawn conditions and spawning location problems. 1 Answer
enemy spawning when dead 1 Answer
Network spawning 1 Answer