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 /
  • Help Room /
avatar image
0
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
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

147 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 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

need help in making an object pooler 1 Answer

Getting an Exception on Endless Runner 3D game 1 Answer

How do I get player to react differently on different pooled objects (clones) that are untagged (How do i tag them)? 0 Answers

How do I make a background continuous of endless with player movement? 1 Answer

Wondering best way to change direction of character in Endless Runner gsme 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