Okay am really new to coding with unity. I basically code in JavaScript but I had to try to understand some piece of code because I wanted to create an endless road which will randomly select between prefabs and create an endless road like temple run.
I want to use this for a competition I need to win so I really need your help Guys please!! Thanks in advance.
Here is the code I attached to an empty game object and assigned a prefab plane with texture and some trees.
using UnityEngine;
using System.Collections.Generic;
public class RandomPlaneGen : MonoBehaviour {
public Transform prefab;
public int numberOfObjects;
public float recycleOffset;
public Vector3 startPosition;
private Vector3 nextPosition;
private Queue<Transform> objectQueue;
void Start () {
objectQueue = new Queue<Transform>(numberOfObjects);
nextPosition = startPosition;
for (int i = 0; i < numberOfObjects; i++) {
Transform o = (Transform)Instantiate(prefab);
o.localPosition = nextPosition;
nextPosition.x += o.localScale.x;
objectQueue.Enqueue(o);
}
}
void Update () {
if (objectQueue.Peek().localPosition.x + recycleOffset < Runner.distanceTraveled) {
Transform o = objectQueue.Dequeue();
o.localPosition = nextPosition;
nextPosition.x += o.localScale.x;
objectQueue.Enqueue(o);
}
}
}
:(The problem is my runner runs infinity like I wanted. But how can I create different planes and make the planes
to be choose randomly between a set of planes I created. I know that is not the best code to do that so i stand corrected.
I added this code to the runner to check his position at runtime.
using UnityEngine;
public class Runner : MonoBehaviour {
public static float distanceTraveled;
void Update(){
transform.Translate(0f,0f,2f*Time.deltaTime);
distanceTraveled = transform.localPosition.x; // Tells us the current position of the object
}
}
Thanks Guys.