MiniGames/Assets/Scripts/SkyWalker/Skywalker_HorizontalPlatformSpawner.cs

45 lines
1.2 KiB
C#

using UnityEngine;
using System.Collections.Generic;
public class Skywalker_HorizontalPlatformSpawner : MonoBehaviour
{
[Header("Spawner Settings")]
public GameObject platformPrefab;
public int initialPlatforms = 5;
public float platformWidth = 155f; // width of each prefab
public Transform player;
public float safeZone = 30f; // how far ahead to spawn
private float spawnX = 0.0f;
private List<GameObject> activePlatforms = new List<GameObject>();
void Start()
{
for (int i = 0; i < initialPlatforms; i++)
SpawnPlatform();
}
void Update()
{
// If player has moved past the next spawn point - safeZone
if (player.position.x + safeZone > spawnX - (initialPlatforms * platformWidth))
{
SpawnPlatform();
DeletePlatform();
}
}
void SpawnPlatform()
{
GameObject go = Instantiate(platformPrefab, new Vector3(spawnX, 0, 0), platformPrefab.transform.rotation);
activePlatforms.Add(go);
spawnX += platformWidth; // move to the right for next platform
}
void DeletePlatform()
{
Destroy(activePlatforms[0]);
activePlatforms.RemoveAt(0);
}
}