- Home /
Question by
Stuarttheboy11 · Mar 19, 2020 at 04:36 PM ·
spawninginfinity
this is hard to explain but: How can i infinitely spawn game objects but only as set amounts in the cameras render distance?
Im currently working on a space game where you can travel an infinite distance and populate planets and expand further into the void, however I need to make it so that as you can travel to an infinite amount of planets, solar systems, etc can spawn but they can only spawn in set amounts in the cameras render distance. I already made a script that spawns them infinitely but they spawn continuously in one area, I want them to spawn as a certain amount in the rendered area, but as you move, more will come into view. (sorry if this is confusing, im having a hard time explaining it).
public class solarSystemspawner : MonoBehaviour {
public GameObject SolarSystem;
public int xPos; // x position in the game world
public int zPos; // z position in the game world
public int yPos; // y position in the game world
public int SpawnNumber;
public bool spawningActive;
void Start()
{
StartCoroutine(EnemySpawn());
}
IEnumerator EnemySpawn()
{
while (true) // this makes them spawn forever
{
xPos = Random.Range(-100000, 100000); // this is the set distance that the game object can spawn in randomly
zPos = Random.Range(-100000, 100000); // this is the set distance that the game object can spawn in randomly
yPos = Random.Range(-100000, 100000); // this is the set distance that the game object can spawn in randomly
Instantiate(SolarSystem, new Vector3(xPos, yPos, zPos), Quaternion.identity); // tells the game where to put the game object
yield return new WaitForSeconds(0.5f); // no reason for this, just to slow it down
SpawnNumber += 1;
}
}
}
Comment