Hi i have a script which spawns objects randomly outside the viewport. Now i want the objects spawned on the right side to start moving to a random position on the left side of the outside of viewport. My problem with the code right now is that the new random position to move to gets updated every frame and the objects keep changing their end position so they move haphazardly. What i want is that each object has its own random position to move to rather than all changing their end position each time i get another random position to move to.
I hope I am clear.
This is my script
***public IEnumerator SpawnOutsideViewPort ()*** {
//yield return new WaitForSeconds (1.0f);
counting = true;
while (timeLeft > 0) {
GameObject ball = balls [1];
Quaternion spawnRotation = Quaternion.identity;
float rand = Random.value;
if(rand <= 1)
{
rightSpawned = Instantiate (ball, getRandomPositionAtRightsideOfViewPort(), spawnRotation) as GameObject;
rightSpawned.name = "rightSpawned";
StartCoroutine (MoveToLeftsideViewPort (rightSpawned));
}
if(rand <= 0.5 && rand >0.25)
{
GameObject leftSpawned = Instantiate (ball, getRandomPositionAtLeftsideOfViewPort(), spawnRotation)as GameObject;
leftSpawned.name = "leftSpawned";
}
if(rand <= 0.75 && rand >0.5)
{
GameObject topSpawned = Instantiate (ball, getRandomPositionAtTopsideOfViewPort(), spawnRotation)as GameObject;
topSpawned.name = "topSpawned";
}
if(rand <= 1 && rand >0.75)
{
GameObject botSpawned = Instantiate (ball, getRandomPositionAtBotsideOfViewPort(), spawnRotation)as GameObject;
botSpawned.name = "botSpawned";
}
yield return new WaitForSeconds (Random.Range (1.0f, 2.0f));
}
}
***public IEnumerator MoveToLeftsideViewPort (GameObject rightSpawned) {***
counting = true;
while (timeLeft > 0) {
rightSpawned.transform.position = Vector3.MoveTowards (rightSpawned.transform.position, getRandomPositionAtLeftsideOfViewPort(), step);
yield return new WaitForSeconds (0);
}
}
↧