I created an object that has to go back and forth tamite Mathf.PingPong. I noticed that if I create instances of this object at a distance of a certain period of time, the function is already in action PingPong beginning. How come? I can solve this problem?
PingPong
function Start(){
}
function Update () {
// Set the x position to loop between 0 and 3
transform.position = Vector3(
Mathf.PingPong(Time.time, 30 ), transform.position.y, transform.position.z);
}
Instantiate
var speed : int = 2;
var chrismasBall : GameObject; //GameObject
var instanza:GameObject[]; //instanza
//var texBall : Texture[] = new Texture [5]; //Array di texture
var score : int = 0;
function Start () {
yield WaitForSeconds(5);
StartCoroutine(Creation(10)); //Coroutine
}
function Update () {
//Physics.gravity = Vector3(0, -0.05, 0); // aggiungo gravità
/*if(instanza==null){
print("cancello");
}
*/
}
function Creation(tempo){
while(true){ //finchè è vero esegui il ciclo
var posizione : Vector3 = Vector3 (Random.Range(-1.9,1.9),15,10.0);
var pallaNuova : int = Random.Range(0,instanza.length);
Instantiate (instanza[pallaNuova], transform.position+posizione, transform.rotation); //instanza chrismasBall
//instanza.AddComponent(Rigidbody); //aggiungo rigidbody alle instanze
/*
instanza.transform.position.x = Random.Range(2,-2); //raggio di instanza
instanza.transform.position.y = 2;
instanza.transform.position.z = 0.2; //distanza dalla Main Camera
instanza.transform.Translate(0, Time.deltaTime, 0);
*/
yield WaitForSeconds(tempo);
}
}