Hello everyone
This is my first post so I apologize if my problem is unclear. I’m following this tutorial Runner, a Unity C# Tutorial
and I just wanted to add spacing between the cubes so i made two spacing var…one normal and one big… The problem i’m having is once the first cube is dequeued and requeued at the end of the line it still has normal spacing for some reason but every cube after that has the big spacing…I’ve found out that changing the recycleOffset to 0 fixing this but then the first cube is dequeued soon as my play reaches it.
Can someone help explain to me what’s going wrong? Sadly I am NO programmer.
public class Skyline : MonoBehaviour
{
public Transform prefab;
public int numberOfObjects;
public float recycleOffset;
public float space;
public float bigSpace;
private Vector3 nextPosition;
private Queue<Transform> objectQueue;
void Start ()
{
objectQueue = new Queue<Transform>(numberOfObjects);
nextPosition = transform.localPosition;
for(int i = 0; i < numberOfObjects; i++)
{
Setup();
}
}
void Update ()
{
if(objectQueue.Peek().localPosition.x + recycleOffset <= Runner.distanceTraveled)
{
Recycle ();
}
}
void Setup()
{
Transform o = (Transform)Instantiate(prefab);
o.localPosition = nextPosition;
nextPosition.x += o.localScale.x + space;
objectQueue.Enqueue(o);
}
void Recycle()
{
Transform o = objectQueue.Dequeue();
o.localPosition = nextPosition;
nextPosition.x += o.localScale.x + bigSpace;
objectQueue.Enqueue(o);
}
}
Thank You
[edit] Code tags added by admin. Please use code tags properly.