Question by
sydbarett · Jul 26, 2018 at 06:14 AM ·
endless runnerobject pool
platform generation for endless runner using object pooling
Hello everyone, I have been working on my own to make an endless 2D runner. I am kind of stuck in platform generation, where I want to generate platform in three levels i.e Bottom, mid and top. Right now I am able to generate platforms at the bottom at a Random position, but when I generate platforms at mid and top, game kind of lags.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class ObjectPoolItem
{
public GameObject objectToPool;
public int amountToPool;
public bool shouldExpand = true;
public ObjectPoolItem(GameObject obj, int amt, bool exp = true)
{
objectToPool = obj;
amountToPool = Mathf.Max(amt,2);
shouldExpand = exp;
}
}
public class ObjectPooler : MonoBehaviour
{
public static ObjectPooler SharedInstance;
public List<ObjectPoolItem> itemsToPool;
public List<List<GameObject>> pooledObjectsList;
public List<GameObject> pooledObjects;
private List<int> positions;
void Awake()
{
SharedInstance = this;
pooledObjectsList = new List<List<GameObject>>();
pooledObjects = new List<GameObject>();
positions = new List<int>();
for (int i = 0; i < itemsToPool.Count; i++)
{
ObjectPoolItemToPooledObject(i);
}
}
public GameObject GetPooledObject(int index)
{
int curSize = pooledObjectsList[index].Count;
for (int i = positions[index] + 1; i < positions[index] + pooledObjectsList[index].Count; i++)
{
if (!pooledObjectsList[index][i % curSize].activeInHierarchy)
{
positions[index] = i % curSize;
return pooledObjectsList[index][i % curSize];
}
}
if (itemsToPool[index].shouldExpand)
{
GameObject obj = (GameObject)Instantiate(itemsToPool[index].objectToPool);
obj.SetActive(false);
obj.transform.parent = this.transform;
pooledObjectsList[index].Add(obj);
return obj;
}
return null;
}
public List<GameObject> GetAllPooledObjects(int index)
{
return pooledObjectsList[index];
}
public int AddObject(GameObject GO, int amt = 3, bool exp = true)
{
ObjectPoolItem item = new ObjectPoolItem(GO, amt, exp);
int currLen = itemsToPool.Count;
itemsToPool.Add(item);
ObjectPoolItemToPooledObject(currLen);
return currLen;
}
void ObjectPoolItemToPooledObject(int index)
{
ObjectPoolItem item = itemsToPool[index];
pooledObjects = new List<GameObject>();
for (int i = 0; i < item.amountToPool; i++)
{
GameObject obj = (GameObject)Instantiate(item.objectToPool);
obj.SetActive(false);
obj.transform.parent = this.transform;
pooledObjects.Add(obj);
}
pooledObjectsList.Add(pooledObjects);
positions.Add(0);
}
}
The above is code for ObjectPooling. and below is my logic for platform generation, right now it is generating platform only at bottom level.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawn : MonoBehaviour {
public Transform generationPoint;
private float distanceBetween;
public float distanceBetweenMin;
public float distanceBetweenMax;
private float width;
ObjectPooler objectPool;
void Start()
{
objectPool = ObjectPooler.SharedInstance;
}
void Update()
{
if (transform.position.x < generationPoint.position.x)
{
distanceBetween = Random.Range(distanceBetweenMin, distanceBetweenMax);
int index = Random.Range(0, 3);
width = objectPool.GetPooledObject(index).GetComponent<BoxCollider2D>().size.x;
transform.position = new Vector3(transform.position.x + (width / 2) + distanceBetween, transform.position.y, transform.position.z);
GameObject newPlatform = objectPool.GetPooledObject(index);
newPlatform.transform.position = transform.position;
newPlatform.transform.rotation = transform.rotation;
newPlatform.SetActive(true);
transform.position = new Vector3(transform.position.x + (width / 2), transform.position.y, transform.position.z);
}
}
}
[1]: /storage/temp/121546-untitled.png
This is the way how I want to generate platforms, i.e whenever there is a gap between the two bottom level platform a mid-level platform is generated at some height to fill this Gap.
All the help is appreciated and i new to unity. :)
untitled.png
(52.4 kB)
Comment