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++;
}
}
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 instead 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!
– Geta-VeYes 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.
– whydoidoitWell, I just tried your method, without the FixedUpdate function and it seems to be working brilliantly! My 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. :)
– Geta-VeJust saw your previous comment! :) I think I understand. I will just have to get my head around coroutines. My goal is basically a Cube Runner style game (old iOS game) where the goal is to simply dodge oncoming 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!
– Geta-Vehi 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
– sid4