Random Generator Instantiate Tiles C# = PLEASE HELP =
Hello, I'm trying create map generator never end but instantiate tiles when Player move Up, Down, Left, Right ..Simply it is cube that will instantiate tiles like you see in the image or you can reply with batter idea.
I have created the game before in Construct 2 engine and this is the video to understood me more : link text
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class GeneratorControler : MonoBehaviour {
public GameObject[] tiles;
public GameObject Holder;
public List<Vector3> createdTiles;
public int tileAmount;
public int tileSize;
public float chanceUp;
public float chanceRight;
public float chanceDown;
public int RandomChooser;
public float TimeNow;
void Start () {
StartCoroutine (GenerateLevel ());
}
void Update()
{
StartCoroutine (Randomly ());
}
IEnumerator Randomly()
{
if (RandomChooser == 0) {
yield return new WaitForSeconds (1f);
{
tileSize = 3;
}
}
if (RandomChooser == 1) {
yield return new WaitForSeconds (1f);
{
tileSize = Random.Range (3, 21);
}
}
}
IEnumerator GenerateLevel ()
{
for(int i = 0; i < tileAmount; i++)
{
float dir = Random.Range(0f,1f);
int tile = Random.Range (0, tiles.Length);
CreateTile (tile);
CallMoveGen(dir);
yield return new WaitForSeconds (TimeNow);
}
yield return 0;
}
void CallMoveGen (float ranDir)
{
if (ranDir < chanceUp) {
MoveGen (0);
} else if (ranDir < chanceRight) {
MoveGen (1);
} else if (ranDir < chanceDown) {
MoveGen (2);
} else {
MoveGen (3);
}
}
void MoveGen(int dir)
{
switch (dir) {
case 0:
transform.position = new Vector3 (transform.position.x, transform.position.y + tileSize, 0);
break;
case 1:
transform.position = new Vector3 (transform.position.x + tileSize , transform.position.y, 0);
break;
case 2:
transform.position = new Vector3 (transform.position.x, transform.position.y - tileSize, 0);
break;
case 3:
transform.position = new Vector3 (transform.position.x - tileSize, transform.position.y + tileSize, 0);
break;
}
}
void CreateTile( int tileIndex)
{
if (!createdTiles.Contains(transform.position))
{
GameObject tileObject;
tileObject = Instantiate (tiles[tileIndex], transform.position, transform.rotation) as GameObject;
tileObject.transform.parent = Holder.transform;
createdTiles.Add(tileObject.transform.position);
}
else {
tileAmount++;
}
}
}
all.png
(8.3 kB)
Comment