- Home /
 
Platform generation using object pooling
I am working on a 2d platformer game, in which I want to generate platforms procedurally in three levels i.e. bottom, mid and top.
Below code is for Object pooling:
 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);
  }}
 
               Code for platformSpawn:
 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);
         width = objectPool.GetPooledObject(0).GetComponent<BoxCollider2D>().size.x;
         transform.position = new Vector3(transform.position.x + (width / 2) + distanceBetween, transform.position.y, transform.position.z);
         GameObject newPlatform = objectPool.GetPooledObject(0);
         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);
     }
 }
 
               The above code is for PlatformSpawn and is my try, the problem is that I am not getting an idea how to generate the platforms procedurally. Below is the image of how I want platform generation to be i.e whenever there is a gap between the bottom ground platforms then a large midground platform should spawn and the same should be for the top but randomly Any help is appreciated. 
Your answer
 
             Follow this Question
Related Questions
Hey how can I spawn a platform after the previous one. 0 Answers
2D 360 degress platformer example needed 0 Answers
Define position as lower left corner? 0 Answers
Spawn endless platforms help 0 Answers