Hey,
I am trying to build a tetris clone on my own. I know there are alot of tutorials but I am trying to get things done myself.
I got my prefabs (Blocks) done and also made this prefabs move to the bottom. After that I tried to build the spawn Script, which is attached to my GameController. I used a random.value to determine which block to spawn next. Obviously the instantiate part is repeated quite often, so I decided to make spawn() function. The Script didn’t work that way tho. It works without the function… repeating the instantiate code part in every if statement.
Be aware it looks messy. I tried out alot of stuff and kept changing the code for around 2 hours now.
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour {
public GameObject tBlock;
public GameObject zBlock;
public GameObject secondZBlock;
public GameObject lBlock;
public GameObject secondLBlock;
public GameObject iBlock;
public GameObject sBlock;
public Vector3 spawnValues;
public float startWait;
public float spawnWait;
// Use this for initialization
void Start () {
StartCoroutine(SpawnBlocks());
}
// Update is called once per frame
void Update () {
}
IEnumerator SpawnBlocks()
// Spawns Waves of asteroids
{
yield return new WaitForSeconds(startWait);
//while (true)
// {
for (int i = 0; i < 10; i++)
{
float z = Random.value;
if (z <= 0.2)
{
spawn(tBlock);
}
else if (z <= 0.4 && z > 0.2)
{
spawn(sBlock);
}
else if (z <= 0.6 && z > 0.4)
{
spawn(iBlock);
}
else if (z <= 0.8 && z > 0.6)
{
if (z <= 0.7)
{
spawn(lBlock);
}
else
{
spawn(secondLBlock);
}
}
else
{
if (z <= 0.9)
{
spawn(zBlock);
}
else
{
spawn(secondZBlock);
}
}
}
}
//}
IEnumerator spawn(GameObject x)
{
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate(x, spawnPosition, spawnRotation);
yield return new WaitForSeconds(spawnWait);
}
}