Hello everyone,
I want to build an endless game with 3 levels, with in between an overflow.
To make this more understandable, I have drawn a painting to be more specific:
The player starts at level 1 and floats upwards, trying to dodge objects. During the levels pieces of level(prefabs) are randomly spawned on each other until the overflow is spawned after 30 times of spawning. This goes the same for level 2 and 3.
My question is:
Does anybody have experience with this and any thoughts or ideas what is the most efficient way to do this?
I heard about Object Pooling as I got that as recommendation but I don’t have experience in that. My wish is actually to drag to pieces of level into the inspector as explained above.
I am now a script which is only spawning one kind of level and looks like:
using UnityEngine;
using System.Collections.Generic;
public class LevelManager : MonoBehaviour {
public Transform[] prefabs;
public int numberOfObjects;
public float recycleOffset;
public Vector3 startPosition;
public Vector3 minGap, maxGap;
public float minX, maxX;
private Vector3 nextPosition;
private Queue<Transform> objectQueue;
void Start () {
objectQueue = new Queue<Transform>(numberOfObjects);
for(int i = 0; i < numberOfObjects; i++){
objectQueue.Enqueue((Transform)Instantiate(prefabs[Random.Range(0,prefabs.Length)]));
}
nextPosition = startPosition;
for(int i = 0; i < numberOfObjects; i++){
Recycle();
}
}
void Update () {
if(objectQueue.Peek().localPosition.y + recycleOffset < PlayerMovement.distanceTraveled - 20f){
Recycle();
}
}
private void Recycle () {
Vector3 position = nextPosition;
position.x += 1f;
Transform o = objectQueue.Dequeue();
o.localPosition = position;
objectQueue.Enqueue(o);
nextPosition += new Vector3(
Random.Range(minGap.y, maxGap.y) + 1f,
Random.Range(minGap.x, maxGap.x),
Random.Range(minGap.x, maxGap.x));
if(nextPosition.x < minX){
nextPosition.x = minX + maxGap.x;
}
else if(nextPosition.x > maxX){
nextPosition.x = maxX - maxGap.x;
}
}
}
I am looking forward to your ideas and recommendations