Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlatformGenerator : MonoBehaviour
- {
- public GameObject thePlatform;
- public Transform generationPoint;
- public float distanceBetween;
- private float platformWidth;
- public float distanceBetweenMin;
- public float distanceBetweenMax;
- private int platformSelector;
- private float[] platformWidths;
- public ObjectPooler[] theObjectPools;
- private float minHeight;
- public Transform maxHeightPoint;
- private float maxHeight;
- public float maxHeightChange;
- private float heightChange;
- private CoinGenerator theCoinGenerator;
- public float randomCoinThreshold;
- public float randomSpikeThreshold;
- public ObjectPooler spikePool;
- public float powerupHeight;
- public ObjectPooler powerupPool;
- public float powerupThreshold;
- // Start is called before the first frame update
- void Awake()
- {
- platformWidths = new float[theObjectPools.Length];
- for (int i = 0; i < theObjectPools.Length; i++)
- {
- platformWidths[i] = theObjectPools[i].pooledObject.GetComponent<BoxCollider2D>().size.x;
- }
- minHeight = transform.position.y;
- maxHeight = maxHeightPoint.position.y;
- theCoinGenerator = FindObjectOfType<CoinGenerator>();
- }
- // Update is called once per frame
- void Update()
- {
- if (transform.position.x < generationPoint.position.x)
- {
- distanceBetween = Random.Range(distanceBetweenMin, distanceBetweenMax);
- platformSelector = Random.Range(0, theObjectPools.Length);
- heightChange = transform.position.y + Random.Range(maxHeightChange, -maxHeightChange);
- if (heightChange > maxHeight)
- {
- heightChange = maxHeight;
- }
- else if (heightChange < minHeight)
- {
- heightChange = minHeight;
- }
- //powerup
- if (Random.Range (0f, 100f) < powerupThreshold)
- {
- // Calculate the position for spawning the powerup above the platform
- GameObject newPowerup = powerupPool.GetPooledObject();
- newPowerup.transform.position = transform.position + new Vector3(distanceBetween / 2, powerupHeight, 0f);
- newPowerup.SetActive(true);
- }
- // Move to the next platform position
- transform.position = new Vector3(transform.position.x + (platformWidths[platformSelector] / 2) + distanceBetween, heightChange, transform.position.z);
- // Spawn a new platform
- GameObject newPlatform = theObjectPools[platformSelector].GetPooledObject();
- newPlatform.transform.position = transform.position;
- newPlatform.transform.rotation = transform.rotation;
- newPlatform.SetActive(true);
- // Spawn coins on the platform
- if (Random.Range(0f, 100f) < randomCoinThreshold)
- {
- theCoinGenerator.SpawnCoins(new Vector3(transform.position.x, transform.position.y + 1f, transform.position.z));
- }
- // Spawn spikes on the platform
- if (Random.Range(0f, 100f) < randomSpikeThreshold)
- {
- GameObject newSpike = spikePool.GetPooledObject();
- float spikeXPosition = Random.Range(-platformWidths[platformSelector] / 2f + 1.5f, platformWidths[platformSelector] / 2f - 1.5f);
- Vector3 spikePosition = new Vector3(spikeXPosition, 0.5f, 0f);
- newSpike.transform.position = transform.position + spikePosition;
- newSpike.transform.rotation = transform.rotation;
- newSpike.SetActive(true);
- }
- // Move to the next platform position
- transform.position = new Vector3(transform.position.x + (platformWidths[platformSelector] / 2) + platformWidths[platformSelector], transform.position.y, transform.position.z);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement