Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sydbarett · Jul 15, 2018 at 03:06 PM · platformerendless runnerobject pool

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. alt text

untitled.png (52.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

88 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Platform Generation For Endless Runner 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges