- Home /
Spawn moving GameObjects with identical gaps!
So I am trying to spawn platforms with identical gaps. I have different three different kind of sizes of these platforms and I want to spawn them with same gap even they are different size.
public class Spawner : MonoBehaviour
{
private GameObject[] platform;
private bool spawn;
private GameObject rollingEnemy;
Vector3 oldSpawnPos;
GameObject platformClone;
public float spawnTime;
void Start()
{
platform = new GameObject[3];
platform[0] = Resources.Load("Level/Platform1") as GameObject;
platform[1] = Resources.Load("Level/Platform2") as GameObject;
platform[2] = Resources.Load("Level/Platform3") as GameObject;
spawn = true;
spawnTime = 1.5f;
oldSpawnPos = Vector3.zero;
}
void Update()
{
// Spawns new platform
if(spawn)
{
StartCoroutine(spawnPlatforms());
}
}
IEnumerator spawnPlatforms()
{
spawn = false;
int randomPlatform = Random.Range(0, 2);
// New platform
platformClone = Instantiate(platform[randomPlatform], Vector3.zero, Quaternion.identity) as GameObject;
platformClone.transform.position = new Vector3(oldSpawnPos.x + (platformClone.GetComponent<BoxCollider>().size.x / 2) + 5f, 0f, 0f);
oldSpawnPos = platformClone.transform.position;
yield return new WaitForSeconds(spawnTime);
spawn = true;
}
}
Here is also wonderful picure what I'm trying to do :D! 
Thanks already for answering! Every answer is closer to the solving the problem!
Answer by akitsu · Nov 26, 2013 at 04:20 PM
I did got it work, if anyone want same kind of platform spawning as Kiwi Run or you find in endless runners I can give the code if needed :D
You may explain how you did it now, if someone is looking for the same thing and runs onto this question :)
if you could, please show me how you fixed this problem as it would help me solve another problem
Hi, I am in desperate need of similar code. I am having the same problem. Would you help me out? Id really appreciate it
Your answer