- Home /
Question by
importguru88 · Aug 20, 2016 at 05:38 PM ·
spawnenemy aihowspawnpoints
How do I spawn a certain amount of enemy ai / It's spawning too many enemy ai's at one time
I want spawn for or five enemy ai. I have the enemy ai's spawning in the update function because it was didn't work well in a spawn function I had created only one spawnpoint work . I have two in my scene . I need to slow down the spawning and spawn certain amount in my scene . Have too many enemy ai's in my scene spawning at the same time . Here my code
using UnityEngine;
using System.Collections;
public class EnemiesSpawner : MonoBehaviour {
public GameObject enemy;
public Transform [] spawnPoints;
public float spawnTime = 20f;
public Vector3 spawnValues;
void Start () {
InvokeRepeating("Spawn", spawnTime, spawnTime); //Calls the "Spawn" function every 10 seconds.
}
void Update () {
int spawnPointIndex = Random.Range (0, spawnPoints.Length); for( int spawnCount = spawnPoints.Length - 1 ; spawnCount >= 0 ; --spawnCount )
Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), 1, Random.Range (-spawnValues.z, -spawnValues.z) ) ; // <= here
Comment
Best Answer
Answer by toddisarockstar · Aug 20, 2016 at 10:57 PM
where you looking for something like this?
public GameObject enemy;
public Transform[] spawnPoints;
public Vector3 spawnValues;
public int howoften = 10;
public int howmany = 3;
public float etimer;
void Update () {
etimer = etimer - Time.deltaTime;
if(etimer<0){etimer=howoften;
int i = howmany;
while(i>0){i--;
int spawnPointIndex = Random.Range (0, spawnPoints.Length);
Instantiate(enemy,
spawnPoints[spawnPointIndex].position,
spawnPoints[spawnPointIndex].rotation);
}
}}