I trying to make infinite runner. Ive made a plane which 2x2 and a script which fill place. It works like fill place 3x25 and start move -x coordinate. if some child has x: -2 coord it will destoy, if childcount < 3*25 it instantiate a 3 new. But here somehow little holes
Ive change logic of script, now childs doesnt destroying they just change them position to some coords and move back when childcount < 3*25 but it still buging
I cant find where my mistake do. Havent attach for the code so here is.
P.s. increase gamespeed increase holes like that.
using UnityEngine;
using System.Collections;
public class LevelScript : MonoBehaviour {
public float widht_field;
public int game_field_lenght;
private float NextPosX;
public float gamespeed;
private GameObject LevelEmpty;
private GameObject TmpObj;
private float ZCoord;
private GameObject CollectedFields;
// Use this for initialization
void Start () {
ZCoord = -widht_field;
LevelEmpty = GameObject.Find("LevelEmpty");
GameObject Fields = new GameObject("Fields");
CollectedFields = GameObject.Find("Fields");
SpawnObjects();
FillScene();
NextPosX -= widht_field;
}
// Update is called once per frame
void Update () {
}
void FixedUpdate()
{
Debug.Log(Time.deltaTime);
LevelEmpty.transform.position = new Vector3(LevelEmpty.transform.position.x-(gamespeed*Time.deltaTime),0,0);
CollectedFields.transform.position = new Vector3(200,0,0);
int count = LevelEmpty.transform.childCount - 1;
for (int i = 0; i < count; i++)
{
if (LevelEmpty.transform.GetChild(i).transform.position.x < -2)
{
LevelEmpty.transform.GetChild(i).transform.position = CollectedFields.transform.position;
LevelEmpty.transform.GetChild(i).transform.parent = CollectedFields.transform;
i--;
count--;
}
}
//foreach (Transform field in LevelEmpty.transform)
//{
// if (field.position.x<-2.0F)
// {
// field.gameObject.transform.position = CollectedFields.transform.position;
// field.gameObject.transform.parent = CollectedFields.transform;
// }
//}
for (int i = 0; i < 3; i++)
{
if (LevelEmpty.transform.GetChild(i).transform.position.x < -2.0F)
{
LevelEmpty.transform.GetChild(i).transform.position = CollectedFields.transform.position;
LevelEmpty.transform.GetChild(i).transform.parent = CollectedFields.transform;
}
}
if (LevelEmpty.transform.childCount < (game_field_lenght) * 3)
{
SpawnField();
}
}
void FillScene()
{
for (int i = 0; i <= (game_field_lenght)-1; i++)
{
SpawnField();
NextPosX+=widht_field;
}
}
void SpawnField()
{
for (int i = 0; i < 3; i++)
{
TmpObj = CollectedFields.transform.GetChild(0).gameObject;
TmpObj.transform.parent = LevelEmpty.transform;
TmpObj.transform.position = new Vector3(NextPosX, 0, ZCoord);
ZCoord += widht_field;
}
ZCoord = -widht_field;
}
void SpawnObjects()
{
for (int i = 0; i < (game_field_lenght)*3; i++)
{
GameObject tmp1 = Instantiate(Resources.Load("tank_field"), new Vector3(NextPosX, 0, 0), Quaternion.Euler(270, 0, 0)) as GameObject;
tmp1.transform.parent = CollectedFields.transform;
tmp1.transform.position = Vector3.zero;
}
CollectedFields.transform.position = new Vector3(-200,0,0);
}
}

