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!
