Hi, I want to randomly generate objects with different timing. I have three objects that I want to generate. The code that I use generate all of the 3 objects very fast I want to generate them 1 by 1 randomly with slow speed
#pragma strict
var RedObject : GameObject;
var BlueObject : GameObject;
var GreenObject : GameObject;
var minSpawnTime1 : float = 0.1f;
var maxSpawnTime1 : float = 0.3f;
var minSpawnTime2 : float = 0.4f;
var maxSpawnTime2 : float = 0.6f;
var minSpawnTime3 : float = 0.1f;
var maxSpawnTime3 : float = 0.5f;
var timer : float = 0.0f;
var nextTime1 : float;
var nextTime2 : float;
var nextTime3 : float;
function Start () {
nextTime1 = Random.Range(minSpawnTime1, maxSpawnTime1);
nextTime2 = Random.Range(minSpawnTime2, maxSpawnTime2);
nextTime3 = Random.Range(minSpawnTime3, maxSpawnTime3);
}
function Update ()
{
timer += Time.deltaTime;
if(timer > nextTime1){
Instantiate(RedObject, transform.position + Vector3(-1.817686, 4.976417, 0), Quaternion.Euler(0,0,0) );
timer = 0.0f;
nextTime1 = Random.Range(minSpawnTime1, maxSpawnTime1);
}
if(timer > nextTime2){
Instantiate(BlueObject, transform.position + Vector3(0.8537216, 4.976417, 0), Quaternion.Euler(0,0,0) );
timer = 0.0f;
nextTime2 = Random.Range(minSpawnTime2, maxSpawnTime2);
}
if(timer > nextTime3){
Instantiate(GreenObject, transform.position + Vector3(3.570783, 4.976417, 0), Quaternion.Euler(0,0,0) );
timer = 0.0f;
nextTime3 = Random.Range(minSpawnTime3, maxSpawnTime3);
}
}