- Home /
Spawn Randomly over Time
Well, as simply as the title states, I am looking to spawn a certain prefab, a random number of times (lets say, 0 to 2) every second.
Thus far I am able to spawn my prefab randomly every tick via the FixedUpdate function, and I also know, I think, that I should use Time.deltaTime to work over seconds / frames, but, embarrassingly I can't figure out where or how to utilize it.
Thank you in advance for any help you guys can give!
Creation Script
#pragma strict
var newTower : Rigidbody;
private var towerCount = 0;
function Start () {}
function FixedUpdate ()
{
var towerNum = Random.Range(0,2);
for (var i = 0; i < towerNum; i++)
{
var towerSpawnPosition = Instantiate
(newTower,
Vector3
(
Random.Range(-50,50),
Random.Range(0,0),
Random.Range(180,200)
),
Quaternion.Euler
(
Random.Range(0,0),
Random.Range(0,0),
Random.Range(0,0)
)
);
Debug.Log("Tower has been built!");
towerCount++;
}
}
Answer by whydoidoit · Jul 14, 2012 at 02:09 AM
Just make Start a coroutine and do it there:
#pragma strict
var newTower : Rigidbody;
private var towerCount = 0;
function Start () {
while(true) {
yield new WaitForSeconds(1);
var towerNum = Random.Range(0,2);
for (var i = 0; i < towerNum; i++)
{
var towerSpawnPosition = Instantiate(newTower, Vector3(Random.Range(-50,50),
Random.Range(0,0),Random.Range(180,200)),
Quaternion.identity);
Debug.Log("Tower has been built!");
towerCount++;
}
}
}
This might seem like a really stupid question, but would the elements in the Start function completely replace the FixedUpdate elements? or would they both run?
I feel like you meant for me to simply add to the Start function ins$$anonymous$$d of replacing the FixedUpdate function altogether, due to your coroutine wording, but I can't be certain... (I am quite new, obviously)
Subsequently, thank you so much for taking the time to help me!
Yes for several reasons:
You shouldn't use FixedUpdate anyway unless you are doing physics functions - it isn't necessary and can lead to poor performance.
You really want a coroutine which can accomplish everything you need - we could start a coroutine from Start() and that would be the right thing to do if you wanted to start and stop this behaviour later. The current example uses Start as a coroutine.
Well, I just tried your method, without the FixedUpdate function and it seems to be working brilliantly!
$$anonymous$$y hurdle now will be figuring out how to get them not to spawn at the exact same time (if I increase the random rate to, say, 0,5/10/15 etc)
Thank you so much!! Now to figure out why this all works. :)
Just saw your previous comment! :) I think I understand. I will just have to get my head around coroutines.
$$anonymous$$y goal is basically a Cube Runner style game (old iOS game) where the goal is to simply dodge onco$$anonymous$$g obstacles, in the case, towers. The difficulty will ramp up via faster moving obstacles.
This is mainly scripting learning project for me right now.
I really appreciate your help!
hi whydo idoit" can you tell me the code for this in 2d? I tried converting but cant do it I know its something with vector 3 to 2 issue
please help
thanks
Your answer
Follow this Question
Related Questions
Drop item from airplane 1 Answer
random food spawning 2 Answers
AI spawning areas. What are the ways to make them? 1 Answer
Spawn object from Random Vector3 in array 0 Answers
Endless/infinite runner random enemy spawning multi lane 1 Answer