- Home /
SPAWNING ENEMIES and controlling the amount/interval
I am trying to spawn enemies from the top of the screen and fall down where they are destroyed at the bottom. How can I change my scipt to control the amount of objects that fall and how often they fall? currently I have methods in place but they dont really seem funtional. See my code below.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EnemySpawner : MonoBehaviour { [SerializeField] private float xLimit = 0; //spawns single enemy [SerializeField] private float [] xPositions = null; [SerializeField] private GameObject[] enemyPrefabs = null; [SerializeField] private Wave[] wave = null;
private float currentTime;
List<float> remainingPositions = new List<float>();
private int waveIndex;
float xPos = 0;
int rand;
// Start is called before the first frame update
void Start()
{
currentTime = 0;
remainingPositions.AddRange(xPositions);
}
// Update is called once per frame
void Update()
{
currentTime -= Time.deltaTime;
if(currentTime <= 0)
{
SelectWave();
}
}
void SpawnEnemy(float xPos)
{
//gameObject.transform.GetComponent<Rigidbody2D>().AddForce(Vector2.down*30*Time.deltaTime,ForceMode2D.Force);
int r = Random.Range(0,8); //3 types of enemies so increase if needed
GameObject enemyObj = Instantiate(enemyPrefabs[r], new Vector3(xPos,transform.position.y,0),Quaternion.identity);//can add rotation here
/*string enemyName = "";
if (r == 0) enemyName = "realVirus-removebg-preview";
else if (r == 1) enemyName = "realVirus-removebg-preview (2)";
else if (r == 2) enemyName = "NormalBlue";
else if (r == 3) enemyName = "Bomb";
else if (r == 4) enemyName = "coins";
else if (r == 5) enemyName = "SlowTime";
else if (r == 6) enemyName = "Doubler";
else if (r == 7) enemyName = "Fivex";
GameObject enemy = ObjectPooling.GetPooledObject(enemyName);
enemy.transform.position = new Vector3(xPos, transform.position.y,0);
enemy.SetActive(true);*/
}
private void SelectWave()
{
remainingPositions = new List<float>();
remainingPositions.AddRange(xPositions);
waveIndex = Random.Range(0,wave.Length);
currentTime = wave[waveIndex].delayTime;
if(wave[waveIndex].spawnAmount == 1)
{
xPos = Random.Range(-xLimit,xLimit);
}
else if (wave[waveIndex].spawnAmount > 1)
{
rand = Random.Range(0, remainingPositions.Count);
xPos = remainingPositions[rand];
remainingPositions.RemoveAt(rand); //no overlapping
}
for(int i = 0; i < wave[waveIndex].spawnAmount; i++)
{
SpawnEnemy(xPos);
rand = Random.Range(0, remainingPositions.Count);
xPos = remainingPositions[rand];
remainingPositions.RemoveAt(rand);
}
}
}
[System.Serializable] public class Wave { public float delayTime; public float spawnAmount; },
Answer by Revolution_Game · Nov 20, 2020 at 10:35 AM
First Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
public static int enemyNum = 0;
float spawnTime = 1.0f;
bool spawn = true;
public GameObject blue;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(enemyNum < 5 && spawn)
{
StartCoroutine(spawnEnemy());
}
}
IEnumerator spawnEnemy()
{
//spawn your enemy here
enemyNum += 1;
spawn = false;
yield return new WaitForSeconds(spawnTime);
spawn = true;
}
}
Destroy Enemy Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyEnemy : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter2D(Collider2D other)
{
//your destroy enemy script here
EnemySpawner.enemyNum -= 1;
}
}
Your answer
Follow this Question
Related Questions
How to spawn all humans at once and activate then with time? 1 Answer
How to spawn enemies at different locations and avoid overlapping each other 2 Answers
Enemy Spawn System with time and limit enemy in game 2 Answers
How to prevent multiple spawns from a single spawn point? 1 Answer
How to increase the spawn rate of an object over time? 2 Answers