Please help, Unity bugs or not?
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);
}
}
Answer by Jessespike · Jul 26, 2016 at 06:58 PM
It's likely a bug in your code. Since you've noticed that increasing gamespeed makes the problem worse, I'd look over that. I would bet it happens because the position is multiplied with deltaTime in a FixedUpdate, that just seems incorrect.
Ive wrote this code like in tutorial. Bad part of runner tutorials, that no explanation in it. Can you tell me why its wrong and what need to do to fix it?
Even if i trying to multiply position not with deltaTime. With float 0.2 for example, my field generation still have those holes.
Your answer
![](https://koobas.hobune.stream/wayback/20220612085548im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Missing Shadows on Some Objects 0 Answers
Particle Collision Bug. 1 Answer
Unity Keeps Destroying My Projects. Please help! (Urgent) 1 Answer
Player keeps bumping when walking over to another sprite 1 Answer
Light Shadow problem 1 Answer