c# - Set delay time to spawn object -
i have spawnscript (below) want create function within spawn, can put minimum , maximum delay time object can created inspector.
using system.collections; using system.collections.generic; public class spawncontroller : monobehaviour { public float maxwidth; public float minwidth; public float mintime; public float maxtime; public float ratespawn; private float currentratespawn; public gameobject tubeprefab; public int maxspawntubes; public list<gameobject> tubes; // use initialization void start () { (int = 0; < maxspawntubes; i++) { gameobject temptube = instantiate (tubeprefab) gameobject; tubes.add (temptube); temptube.setactive (false); } currentratespawn = ratespawn; } // update called once per frame void update () { currentratespawn += time.deltatime; if (currentratespawn > ratespawn) { currentratespawn = 0; spawn (); } } private void spawn () { float randwitdh = random.range (minwidth, maxwidth); gameobject temptube = null; (int = 0; < maxspawntubes; i++) { if (tubes [i].activeself == false) { temptube = tubes [i]; break; } } if (temptube != null) temptube.transform.position = new vector3 (randwitdh, transform.position.y, transform.position.z); temptube.setactive (true); } }
you use time.realtimesincestartup
timestamps - kinda fit way atm. or use coroutines unity , 1 better learns use them late. or use invoke shortest way of doing it.
http://docs.unity3d.com/scriptreference/time-realtimesincestartup.html http://docs.unity3d.com/manual/coroutines.html http://docs.unity3d.com/scriptreference/monobehaviour.invoke.html
edit: ratespawn = random.range(mintime, maxtime);
inside if statement in update, fit current approach most.
Comments
Post a Comment