Search Unity

Random Generation Generate Percentage Issue

Discussion in 'Scripting' started by Seremedy, Jun 20, 2018.

  1. Seremedy

    Seremedy

    Joined:
    May 22, 2018
    Posts:
    5
    So I've been making an endless runner and it's been going smoothly. Now that I'm getting higher into learning coding and have gotten decent systems and have been fixing up coding daily to better it. I've started making the random number generator that determines what platforms are generated. I thought I had it right but it's just not working and my mind is pretty much shot. If anyone could help even give me a hint as to what I need to do/use I'd appreciate it

    [Sorry if it's a little messy. I've streamlined and cleaned up most of my coding. I just started this section.]

    Level Generator Code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlatformGeneration : MonoBehaviour
    6. {
    7.     public Transform genPoint;
    8.     private float distBetween;
    9.  
    10.     private float platformWidth;
    11.     public float platformWidthMin;
    12.     public float platformWidthMax;
    13.     private float heightMin;
    14.     public Transform maxHeightPoint;
    15.     public Transform minHeightPoint;
    16.     private float heightMax;
    17.     public float maxHeightChange;
    18.     private float heightChange;
    19.  
    20.     private int platformSelector;
    21.     private float[] platformWidths;
    22.  
    23.     public float minRandomNumber;
    24.     public float maxRandomNumber;
    25.  
    26.     private float randomNumber;
    27.  
    28.     public ObjectPooler[] objectPools;
    29.  
    30.     // Use this for initialization
    31.     void Start ()
    32.     {
    33.         //  platformWidth = thePlatform.GetComponent<BoxCollider2D>().size.x;
    34.         platformWidths = new float[objectPools.Length];
    35.  
    36.         for (int i = 0; i < objectPools.Length; i++)
    37.         {
    38.             platformWidths[i] = objectPools[i].pooledObject.GetComponent<BoxCollider2D>().size.x;
    39.         }
    40.  
    41.         heightMin = minHeightPoint.position.y;
    42.         heightMax = maxHeightPoint.position.y;
    43.     }
    44.  
    45.     // Update is called once per frame
    46.     void Update()
    47.     {
    48.        
    49.  
    50.         if (transform.position.x < genPoint.position.x)
    51.         {
    52.             distBetween = Random.Range(platformWidthMin, platformWidthMax);
    53.             platformSelector = Random.Range(0, objectPools.Length);
    54.             heightChange = transform.position.y + Random.Range(maxHeightChange, -maxHeightChange);
    55.  
    56.             if (heightChange > heightMax)
    57.             {
    58.                 heightChange = heightMax;
    59.             }
    60.             else
    61.             if (heightChange < heightMin)
    62.             {
    63.                 heightChange = heightMin;
    64.             }
    65.             transform.position = new Vector3(transform.position.x + platformWidths[platformSelector] / 2 + distBetween + 3, heightChange, transform.position.z);
    66.  
    67.             randomNumber = Random.Range(0, 100);
    68.  
    69.             if (randomNumber > 0 && randomNumber < 51)
    70.             {
    71.             GameObject newPlatform = objectPools[platformSelector].GetPooledObjectCommon();
    72.             newPlatform.transform.position = transform.position;
    73.             newPlatform.transform.rotation = transform.rotation;
    74.             newPlatform.SetActive(true);
    75.  
    76.             transform.position = new Vector3(transform.position.x + platformWidths[platformSelector] / 2 +2, transform.position.y, transform.position.z);
    77.             }
    78.  
    79.             if (randomNumber > 50 && randomNumber < 81)
    80.             {
    81.                 GameObject newPlatform = objectPools[platformSelector].GetPooledObjectUncommon();
    82.                 newPlatform.transform.position = transform.position;
    83.                 newPlatform.transform.rotation = transform.rotation;
    84.                 newPlatform.SetActive(true);
    85.  
    86.                 transform.position = new Vector3(transform.position.x + platformWidths[platformSelector] / 2 + 2, transform.position.y, transform.position.z);
    87.             }
    88.  
    89.             if (randomNumber > 81 && randomNumber < 101)
    90.             {
    91.                 GameObject newPlatform = objectPools[platformSelector].GetPooledObjectRare();
    92.                 newPlatform.transform.position = transform.position;
    93.                 newPlatform.transform.rotation = transform.rotation;
    94.                 newPlatform.SetActive(true);
    95.  
    96.                 transform.position = new Vector3(transform.position.x + platformWidths[platformSelector] / 2 + 2, transform.position.y, transform.position.z);
    97.             }
    98.         }
    99.     }
    100.     }
    101.  
    Object Pooler

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ObjectPooler : MonoBehaviour
    6. {
    7.     public GameObject pooledObject;
    8.     public int pooledAmount;
    9.     List<GameObject> commonPooledObjects;
    10.     List<GameObject> uncommonPooledObjects;
    11.     List<GameObject> rarePooledObjects;
    12.     public bool test;
    13.  
    14.     public GameObject common;
    15.     public GameObject Uncommon;
    16.     public GameObject rare;
    17.  
    18.     // Use this for initialization
    19.     void Start()
    20.     {
    21.         GameObject.FindGameObjectsWithTag("Common");
    22.         GameObject.FindGameObjectsWithTag("Uncommon");
    23.         GameObject.FindGameObjectsWithTag("Rare");
    24.  
    25.         commonPooledObjects = new List<GameObject>();
    26.         uncommonPooledObjects = new List<GameObject>();
    27.         rarePooledObjects = new List<GameObject>();
    28.  
    29.         for (int i = 0; i < pooledAmount; i++)
    30.             {
    31.             if (gameObject.tag == "Common")
    32.             {
    33.                 GameObject obj = (GameObject)Instantiate(pooledObject);
    34.                 obj.SetActive(false);
    35.                 commonPooledObjects.Add(obj);
    36.             }
    37.             else
    38.             if (gameObject.tag == "Uncommon")
    39.             {
    40.                 GameObject obj = (GameObject)Instantiate(pooledObject);
    41.                 obj.SetActive(false);
    42.                 uncommonPooledObjects.Add(obj);
    43.                 test = true;
    44.             }
    45.             else
    46.             if (gameObject.tag == "Rare")
    47.             {
    48.                 GameObject obj = (GameObject)Instantiate(pooledObject);
    49.                 obj.SetActive(false);
    50.                 rarePooledObjects.Add(obj);
    51.             }
    52.             else
    53.             {
    54.                 break;
    55.             }
    56.  
    57.         }
    58.     }
    59.  
    60.     public GameObject GetPooledObjectCommon()
    61.     {
    62.         for (int i = 0; i < commonPooledObjects.Count; i++)
    63.         {
    64.             if (!commonPooledObjects[i].activeInHierarchy)
    65.             {
    66.                 return commonPooledObjects[i];
    67.             }
    68.         }
    69.         GameObject obj = (GameObject)Instantiate(pooledObject);
    70.         obj.SetActive(false);
    71.         commonPooledObjects.Add(obj);
    72.         return obj;
    73.     }
    74.  
    75.     public GameObject GetPooledObjectUncommon()
    76.     {
    77.         for (int i = 0; i < uncommonPooledObjects.Count; i++)
    78.         {
    79.             if (!uncommonPooledObjects[i].activeInHierarchy)
    80.             {
    81.                 return uncommonPooledObjects[i];
    82.             }
    83.         }
    84.         GameObject obj = (GameObject)Instantiate(pooledObject);
    85.         obj.SetActive(false);
    86.         uncommonPooledObjects.Add(obj);
    87.         return obj;
    88.     }
    89.  
    90.     public GameObject GetPooledObjectRare()
    91.     {
    92.         for (int i = 0; i < rarePooledObjects.Count; i++)
    93.         {
    94.             if (!rarePooledObjects[i].activeInHierarchy)
    95.             {
    96.                 return rarePooledObjects[i];
    97.             }
    98.         }
    99.         GameObject obj = (GameObject)Instantiate(pooledObject);
    100.         obj.SetActive(false);
    101.         rarePooledObjects.Add(obj);
    102.         return obj;
    103.     }
    104. }
    105.  
     
  2. Seremedy

    Seremedy

    Joined:
    May 22, 2018
    Posts:
    5
    It won't let me edit the post but here is more on it.

    I am trying to get the Object Pooler to put an object into the list it goes with. So uncommon tags go in the uncommon list. And Common tagged objects go into the common list.

    Then when the randomNumber is equal to the range for common in the Generator file it will use the correct list from Object Pooler.

    Right now it puts common tagged objects in with the rare list. Or mostly it doesn't even put them into a list.
     
  3. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    I don't see where are you assigning results of .FindGameObjectsWithTag();
    Also, use .CompareTag() instead of == operator.

    Few tips:
    Find is pretty slow. Why not just inverse control it? Put a method that returns your objects to pool at your objects script. That way you would reliably get platforms back when they're available.
    You can also define an enum to determine rarity, and do a switch check, that would cut tag comparison as well.

    Something like:

    Code (CSharp):
    1. // PseudoCode
    2. public enum Rarity {
    3.    Default = 0,
    4.    Common = 1,
    5.    Uncommon = 2,
    6.    Rare = 3,
    7. }
    8.  
    9. // Then in your platform.cs or whatever:
    10. public Rarity Rarity = Rarity.Common; // Or set it via inspector
    11.  
    12. void Start(){
    13.   // I'm ready
    14.   // ObjectPooler.ReturnToPool(gameObject or this, Rarity.X);
    15. }
    Randomization code can be simplified immensely, just introduce a method that performs operations on the pooled object. Google what DRY is. That will help you in the future.
     
    Seremedy likes this.
  4. Seremedy

    Seremedy

    Joined:
    May 22, 2018
    Posts:
    5

    I'll try that, thanks dood! Yeah I just started getting comfortable with methods and such. So this will definitely be new and fun to try out. And the FindObjectsWithTag was something I tried out and found it wasn't the cleanest way or event the way to do what I wanted. I just didn't remove it yet.