- Home /
Spawn gameobject within a time window
In my project i am trying to get a gameObject to spawn randomly within a window of time. Every second, the gameObject should be spawned randomly between 0 and 1 seconds. A new game object cannot spawn until the entire second is up.
Here is my demo: http://soylentworks.com/codesucks/Builds/TouchnDrag.html You can see why i want to offset the time at which things spawn, since all 3 game objects come out at the same time.
In my code i created spawnWindow, and spawnWindowOpen to help with this, but i keep getting the wrong results. Read code comments for more detail.
Here is my current code for the Spawner object:
using UnityEngine;
using System.Collections;
public class piSpawner : MonoBehaviour {
public GameObject follower; // A single follower defined in prefab editor.
public GameObject[] followers; // A group of followers defined in the prefab editor
public float spawnRate = 1.0f; //The frequency at which followers spawn.
private float nextSpawn = 0.0f; //used to trigger the next spawn
private float nextSpawnWindow = 0.0f; // used to trigger SpawnWindow
int followerIndex; // defines which follower to spawn
float spawnRange; // defines the area in which the follower can spawn relative to th Spawner Object
Vector3 spawnFromPoint; // tells the Spawner where to spawn something
float spawnWindow; // The time interval in which a follower can spawn
bool spawnWindowOpen = false; //a follower can only spawn if true
void Start () {
//this.gameObject.GetComponent<MeshRenderer>.enabled = false;
renderer.enabled = false;
}
void Update () {
followerIndex = Random.Range(0, followers.Length); // Give me a random int from followers[]
spawnRange = Random.Range(0.5f, -0.5f);
spawnFromPoint = new Vector3(0, spawnRange, 0);
//Make followers spawn on a countdown timer
// Every time the SpawnRate ticks, the follower can spawn at anytime between this tick,and the next tick.
if (Time.time > nextSpawn && spawnWindowOpen == true) {
spawnWindow = Random.Range(Time.time, Time.time + spawnRate);
nextSpawnWindow = Time.time + spawnWindow;
GameObject piClone = Instantiate(followers[followerIndex], transform.position + spawnFromPoint, transform.rotation) as GameObject;
spawnWindowOpen = false;
Debug.Log ("***Follower has been spawned***");
}
if (Time.time > nextSpawn && spawnWindowOpen == false) {
nextSpawn = Time.time + spawnRate; // Trigger next spawn
spawnWindowOpen = true;
Debug.Log ("***Ready for the next follower***");
}
}
}
Any help appreciated.
Answer by jillytot · Oct 31, 2013 at 01:47 AM
The solution ended up being a coroutine:
using UnityEngine;
using System.Collections;
public class piSpawner : MonoBehaviour {
public GameObject follower; // A single follower defined in prefab editor.
public GameObject[] followers; // A group of followers defined in the prefab editor
public float spawnRate = 1.0f; //The frequency at which followers spawn.
private float nextSpawn = 0.0f; //used to trigger the next spawn
int followerIndex; // defines which follower to spawn
float spawnRange; // defines the area in which the follower can spawn relative to th Spawner Object
Vector3 spawnFromPoint; // tells the Spawner where to spawn something
bool spawnWindowOpen = false; //a follower can only spawn if true
IEnumerator Start () {
//make the spawner object invisile on game start
renderer.enabled = false;
for(;;) {
float nextSpawnTime = Random.Range(0, spawnRate);
yield return new WaitForSeconds(nextSpawnTime);
followerIndex = Random.Range(0, followers.Length); // Give me a random int from followers[]
spawnRange = Random.Range(0.5f, -0.5f);
spawnFromPoint = new Vector3(0, spawnRange, 0);
//Make followers spawn on a countdown timer
// Every time the SpawnRate ticks, the follower can spawn at anytime between this tick,and the next tick.
GameObject piClone = Instantiate(followers[followerIndex], transform.position + spawnFromPoint, transform.rotation) as GameObject;
spawnWindowOpen = false;
Debug.Log ("***Follower has been spawned***");
yield return new WaitForSeconds(spawnRate - nextSpawnTime);
spawnWindowOpen = true;
Debug.Log ("***Ready for the next follower***");
}
}
}
Your answer
Follow this Question
Related Questions
Calculating interval dependants; ie: Velocity 2 Answers
C# Timer and Spawning Prefabs - 2d Sidescroller 1 Answer
How to stop spawning enemies after countdown timer reaches zero? 1 Answer
Increasing spawn rate over time 1 Answer
Timer-function which calculates how long an enabled script is running yet? 1 Answer