Hello everyone
I have a script to spawn the enemy, how can I get it to increase the spawn over time? and also not exceeding certain amount? if there’s any tutorial or anyone can help would be great
here’s the code.
using UnityEngine;
using System.Collections;
public class EnemySpawningScript : MonoBehaviour {
public GameObject [] enemies;
int enemyNo;
public float maxPos = 2.5f;
public float delayTimer = 0.5f;
float timer;
void Start () {
timer = delayTimer;
}
void Update () {
timer -= Time.deltaTime;
if (timer <=0)
{
Vector3 enemyPos = new Vector3(Random.Range(-2.8f, 2.8f), transform.position.y, transform.position.z);
enemyNo = Random.Range (0,28);
Instantiate (enemies[enemyNo], enemyPos, transform.rotation);
timer = delayTimer;
}
}
}
I’ve never done this but my immediate thought would be to do something like (note, out-of-head and untested):
using UnityEngine;
using System.Collections;
public class EnemySpawningScript : MonoBehaviour {
public GameObject [] enemies;
int enemyNo;
public float maxPos = 2.5f;
private float lastSpawnTime;
private float spawnDelay = 5f; // starting spawn delay, once every 5 second
private float minSpawnDelay = 0.5f; // min delay between spawns, currently 2 times per second
private float spawnDelayAdjustmentFactor = 0.9f; // value to multiply the spawnDelay each time.
//
// Note the spawnDelayAdjustmentFactor could be a value that you subtract from the spawnDelay
// instead of multiplying - it just depends on how you want it to behave.
//
void Start () {
lastSpawnTime = 0;//Time.time;
}
void Update () {
float timeSinceLastSpawn = Time.time - lastSpawnTime;
if (timeSinceLastSpawn >= spawnDelay)
{
lastSpawnTime = Time.time;
if(spawnDelay > minSpawnDelay)
{
spawnDelay = spawnDelay * spawnDelayAdjustmentFactor;
if(spawnDelay < minSpawnDelay)
{
spawnDelay = minSpawnDelay;
}
}
Vector3 enemyPos = new Vector3(Random.Range(-2.8f, 2.8f), transform.position.y, transform.position.z);
enemyNo = Random.Range (0,28);
Instantiate (enemies[enemyNo], enemyPos, transform.rotation);
}
}
}
Thank you very much for you help really appreciated but I’m really bad with math, which is why I’m not sure if what I did was right, I wanted to reduce the spawn time every a min and a half by 1 second until it goes down to a second and half per spawn won’t go below that so I did this to your adjustment on my script not sure if that’s right though can you have a look ?
using UnityEngine;
using System.Collections;
public class SpawningTimerForIncreasingSpawnRate : MonoBehaviour {
public GameObject [] enemies;
int enemyNo;
public float maxPos = 2.5f;
private float lastSpawnTime;
private float spawnDelay = 3f; // starting spawn delay, once every 5 second
private float minSpawnDelay = 90f; // min delay between spawns, currently 2 times per second
private float spawnDelayAdjustmentFactor = 0.1f; // value to multiply the spawnDelay each time.
//
// Note the spawnDelayAdjustmentFactor could be a value that you subtract from the spawnDelay
// instead of multiplying - it just depends on how you want it to behave.
//
void Start ()
{
lastSpawnTime = 0;//Time.time;
}
void Update ()
{
float timeSinceLastSpawn = Time.time - lastSpawnTime;
if (timeSinceLastSpawn >= spawnDelay)
{
lastSpawnTime = Time.time;
if(spawnDelay > minSpawnDelay)
{
spawnDelay = spawnDelay - spawnDelayAdjustmentFactor;
if(spawnDelay < minSpawnDelay)
{
spawnDelay = minSpawnDelay;
}
}
Vector3 enemyPos = new Vector3(Random.Range(-2.8f, 2.8f), transform.position.y, transform.position.z);
enemyNo = Random.Range (0,27);
Instantiate (enemies[enemyNo], enemyPos, transform.rotation);
}
}
}