Search Unity

C# Gauging Procedural Placement Size/How to reduce Resource Intensity

Discussion in 'Scripting' started by kenaochreous, Jul 2, 2016.

  1. kenaochreous

    kenaochreous

    Joined:
    Sep 7, 2012
    Posts:
    395
    I've been working on my own 2d procedural generation script and I'm a little unsure how to gauge how big I should make my Procedural Placement be. Initially I tried gauging it by how fast my 2D character could move. I found out that it could reach position 250 in less than a minute along either the X or Y axes. Next I gradually scaled up my estimate until it would take atleast day to reach from one end of the space I generated which would be position 360000. I then divided 360000 by 2 in order to get negative positions as well. Now I'm at the point where I have to figure out how many gameobjects to generate in the space I've now just created. I figured that the number of gameobjects would have to equal the overall size of the space so I set that 360000 as well. Unfortunately the number of gameobjects being generated was too resource intensive for my PC to process. Does Anyone have any suggestions for how I could make my procedural placement more manageable and less resource intensive?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. [System.Serializable]
    6. public class TrackObstacle : MonoBehaviour {
    7.  
    8.     public GameObject gam;
    9.  
    10.     public List<GameObject> poolTrack;
    11.     public int maxPoolSize = 3600000;
    12.     public List<GameObject> obstacles;
    13.     public List<float> probabs;
    14.  
    15.     void Awake() {
    16.     seedobstacle();
    17.     }
    18.  
    19.     public void Recycle(){
    20.         for(int j=0; j<poolTrack.Count; ++j){
    21.             Destroy (poolTrack[j]);
    22.         }
    23.         poolTrack = new List<GameObject>();
    24.     }
    25.     public void seedobstacle() {
    26.         for(int i = 0; i < maxPoolSize; i++){
    27.             Vector3 pos = gam.transform.TransformPoint(Random.Range(-180000,180000),Random.Range(-180000,180000),0);
    28.             int randtrack=Probability(probabs,obstacles.Count);
    29.             poolTrack.Add((GameObject)Instantiate(obstacles[randtrack],pos,Quaternion.identity));
    30.         }
    31.     }
    32.  
    33.     int Probability(List<float> probabs, int size){
    34.         float sum=0f;
    35.         for (int i=0; i<size;++i) {
    36.             sum+=probabs[i];
    37.         }
    38.         float value = sum * Random.value;
    39.         float sum2=probabs[0];
    40.         for ( int i=0; i< size; ++i){
    41.             if(sum2>value){
    42.                 return 0;
    43.             }else if(size-i==1){
    44.                 return i;
    45.             }
    46.             else if( value>sum2&&value<sum2+probabs[i+1]){
    47.                 return i+1;
    48.             }
    49.             sum2+=probabs[i+1];
    50.         }
    51.         return 0;
    52.     }
    53. }
     
  2. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    Instead of generating every obstacle at runtime. You should set up a "chunk" system so that only visible chunks have generated obstacles. This is similar to voxel terrain generation which you should research.