Advertisement
johnnygoodguy2000

PlatformGenerator.cs

Apr 7th, 2024 (edited)
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.09 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlatformGenerator : MonoBehaviour
  6. {
  7.     public GameObject thePlatform;
  8.     public Transform generationPoint;
  9.     public float distanceBetween;
  10.  
  11.     private float platformWidth;
  12.  
  13.     public float distanceBetweenMin;
  14.     public float distanceBetweenMax;
  15.  
  16.     private int platformSelector;
  17.  
  18.     private float[] platformWidths;
  19.  
  20.     public ObjectPooler[] theObjectPools;
  21.  
  22.     private float minHeight;
  23.     public Transform maxHeightPoint;
  24.     private float maxHeight;
  25.     public float maxHeightChange;
  26.     private float heightChange;
  27.  
  28.     private CoinGenerator theCoinGenerator;
  29.  
  30.     public float randomCoinThreshold;
  31.  
  32.     public float randomSpikeThreshold;
  33.     public ObjectPooler spikePool;
  34.  
  35.     public float powerupHeight;
  36.     public ObjectPooler powerupPool;
  37.  
  38.     public float powerupThreshold;
  39.    
  40.  
  41.     // Start is called before the first frame update
  42.     void Awake()
  43.     {
  44.         platformWidths = new float[theObjectPools.Length];
  45.  
  46.         for (int i = 0; i < theObjectPools.Length; i++)
  47.         {
  48.             platformWidths[i] = theObjectPools[i].pooledObject.GetComponent<BoxCollider2D>().size.x;
  49.         }
  50.  
  51.         minHeight = transform.position.y;
  52.         maxHeight = maxHeightPoint.position.y;
  53.  
  54.         theCoinGenerator = FindObjectOfType<CoinGenerator>();
  55.     }
  56.  
  57.     // Update is called once per frame
  58.     void Update()
  59.     {
  60.         if (transform.position.x < generationPoint.position.x)
  61.         {
  62.             distanceBetween = Random.Range(distanceBetweenMin, distanceBetweenMax);
  63.             platformSelector = Random.Range(0, theObjectPools.Length);
  64.  
  65.             heightChange = transform.position.y + Random.Range(maxHeightChange, -maxHeightChange);
  66.  
  67.             if (heightChange > maxHeight)
  68.             {
  69.                 heightChange = maxHeight;
  70.             }
  71.             else if (heightChange < minHeight)
  72.             {
  73.                 heightChange = minHeight;
  74.             }
  75.  
  76.             //powerup
  77.             if (Random.Range (0f, 100f) < powerupThreshold)
  78.             {
  79.                 // Calculate the position for spawning the powerup above the platform
  80.                 GameObject newPowerup = powerupPool.GetPooledObject();
  81.                 newPowerup.transform.position = transform.position + new Vector3(distanceBetween / 2, powerupHeight, 0f);
  82.                 newPowerup.SetActive(true);
  83.             }
  84.  
  85.             // Move to the next platform position
  86.             transform.position = new Vector3(transform.position.x + (platformWidths[platformSelector] / 2) + distanceBetween, heightChange, transform.position.z);
  87.  
  88.             // Spawn a new platform
  89.             GameObject newPlatform = theObjectPools[platformSelector].GetPooledObject();
  90.             newPlatform.transform.position = transform.position;
  91.             newPlatform.transform.rotation = transform.rotation;
  92.             newPlatform.SetActive(true);
  93.  
  94.             // Spawn coins on the platform
  95.             if (Random.Range(0f, 100f) < randomCoinThreshold)
  96.             {
  97.                 theCoinGenerator.SpawnCoins(new Vector3(transform.position.x, transform.position.y + 1f, transform.position.z));
  98.             }
  99.  
  100.             // Spawn spikes on the platform
  101.             if (Random.Range(0f, 100f) < randomSpikeThreshold)
  102.             {
  103.                 GameObject newSpike = spikePool.GetPooledObject();
  104.                 float spikeXPosition = Random.Range(-platformWidths[platformSelector] / 2f + 1.5f, platformWidths[platformSelector] / 2f - 1.5f);
  105.                 Vector3 spikePosition = new Vector3(spikeXPosition, 0.5f, 0f);
  106.                 newSpike.transform.position = transform.position + spikePosition;
  107.                 newSpike.transform.rotation = transform.rotation;
  108.                 newSpike.SetActive(true);
  109.             }
  110.  
  111.             // Move to the next platform position
  112.             transform.position = new Vector3(transform.position.x + (platformWidths[platformSelector] / 2) + platformWidths[platformSelector], transform.position.y, transform.position.z);
  113.         }
  114.     }
  115. }
  116.  
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement