- Home /
random enemy spawn from day night cycle
hello im lookin for some tips on spawning enemies by time of day my goal is to.. the enemies spawn random locations say 1000 enemies spawn per hour when the hour is up checks remaining enemies and re populations random locations across the map up to 1000 in total ( in the hour 100 died so it respawns 100 to keep the 1000 quota ) and also have it so at night it increases the spawn amount to say 2000 per hour then when its morning again back to 1000 per hour.. iv read abunch of answers to questions about enemies and day night cycle and i havnt tried to put what iv learned to test yet... i thought id ask maybe get a general outline of how to go about my specific question instead of trying to do some mix match things iv picked up along the way.. thanks for the help hope to hear some thoughts soon
Answer by Justin Warner · Mar 25, 2011 at 07:36 PM
Just keep a tally of how many enemies their are, and in the awake function check to see if it's above 1000, if it is, then "destroy" all the extras, and if it's less than 1000, make a new var, and have it loop instantiate those... You can try it in the update function, but I'm thinking more of the second you start the scene, it should populate.
Um, and then just have a time function that gets the time, and check it... Could get this from the internet (WWW stuff)< or it could be something you make yourself: http://www.google.com/search?client=opera&rls=en&q=time+in+unity3d&sourceid=opera&ie=utf-8&oe=utf-8&channel=suggest
But maybe this'll help a little?
Answer by Niklas · Mar 25, 2011 at 08:13 PM
I Wrote a bunch of code. Hope this is helpfull.
This registers each spawnPoint to a SpawnMaster. This spawn master ticks time and triggers Repopulate on a random sub-list of ISpawnPoint's at each time interval.
The spawn point could instatiate objects after a set of rules that you define, but for now i only make it really basic. (Not really a working example, but mostly to giva a hint of what you could do) :)
For now this only divides the number of creatures even among selected spawn points and repopulates them. This behaviour could be change by changing the selection behaviour to take into account the number of creatures currently on each spawn point, but i will leave that algorithm to you since i dont really know how this spawning should happen.
Hopefully this examples can help you on your way. :)
Regards,
( The code is totally untested so there might be errors )
SpawnMaster interface
public interface ISpawnMaster
{
void AddSpawnPoint(ISpawnPoint point);
}
SpawnMaster
public class SpawnMaster : MonoBehaviour, ISpawnMaster { public int NumRandomSpawnPoints = 5; public int Quota = 1000; public float SpawnInterval = 5; private float spawnTimer = 0.0f;
private List<ISpawnPoint> _spawnPoints;
void Awake()
{
gameObject.tag = "SpawnMaster";
}
void Start()
{
_spawnPoints = new List<ISpawnPoint>();
}
void Update()
{
spawnTimer += Time.deltaTime;
if (spawnTimer > SpawnInterval)
{
spawnTimer -= SpawnInterval;
FillQuota();
}
}
public void AddSpawnPoint(ISpawnPoint point)
{
_spawnPoints.Add(point);
}
void FillQuota()
{
if (Quota > 0)
{
if (_spawnPoints.Count > NumRandomSpawnPoints)
{
List<ISpawnPoint> selectedPoints = new
List<ISpawnPoint>();
while (selectedPoints.Count !=
NumRandomSpawnPoints)
{
foreach (ISpawnPoint point in _spawnPoints)
{
if (!selectedPoints.Contains(point))
{
if (Random.Range(0, 1) > 0)
{
selectedPoints.Add(point);
}
}
}
}
foreach (ISpawnPoint point in selectedPoints)
{
SpawnList(selectedPoints);
}
}
else
{
SpawnList(_spawnPoints);
}
}
}
void SpawnList(List<ISpawnPoint> list)
{
foreach (ISpawnPoint point in list)
{
point.Repopulate(list.Count / Quota);
}
}
}
SpawnPoint interface
public interface ISpawnPoint
{
void Repopulate(int num);
int NumCreatures();
}
Creature
class Creature : MonoBehaviour
{
public string CreatureName = "Dangerous evil monster";
public bool Enabled = true;
}
Creature spawner (SpawnPoint)
public class CreatureSpawner : MonoBehaviour, ISpawnPoint { private List<Creature> _creatures;
// Use this for initialization void Start () { _creatures = new List<Creature>(); // Not sure you can find interface using this method, but if //you cant, you'll probably figure it out (GameObject.FindObjectOfType(typeof(ISpawnMaster)) as ISpawnMaster).AddSpawnPoint(this); }
void Update()
{
// You wont need to do this each update. A better way could
// be a callback when creature dies
foreach (Creature creature in _creatures)
{
if (!creature.Alive)
{
// kill and remove
}
}
}
public int NumCreatures()
{
return _creatures.Count;
}
public void Repopulate(int num)
{
for (int i = 0; i < num; ++i)
{
_creatures.Add(new Creature());
}
}
}
Your answer
Follow this Question
Related Questions
c# Enemy's spawning over time, problem 2 Answers
More Efficient Enemy Spawning Than This? 2 Answers
How to make an enemy attack every few seconds 1 Answer
Trying out a unique method of spawning enemies. Someone want to help sort out the logic of it? 1 Answer
How can I spawn an enemy "behind the nearest corner"? 1 Answer