MiniGames/Assets/Skywalker_WallPool.cs

50 lines
1.4 KiB
C#
Raw Normal View History

2025-09-01 00:01:33 +05:00
using UnityEngine;
public class Skywalker_WallPool : MonoBehaviour
{
public GameObject wallPrefab; // The brick wall prefab
public int poolSize = 3; // Start with 3 walls
public float wallWidth = 10f; // Width of each wall (adjust to prefab size)
public Transform cameraTransform; // Reference to camera/player
private GameObject[] walls;
private int nextWallIndex = 0;
void Start()
{
// Initialize pool
walls = new GameObject[poolSize];
for (int i = 0; i < poolSize; i++)
{
Vector3 pos = new Vector3(i * wallWidth, 0f, 0f);
walls[i] = Instantiate(wallPrefab, pos, Quaternion.identity);
}
}
void Update()
{
// Check if camera has passed the second wall
for (int i = 0; i < poolSize; i++)
{
if (cameraTransform.position.x > walls[i].transform.position.x + wallWidth)
{
// Move wall ahead of the last wall
float newX = GetFurthestWallX() + wallWidth;
walls[i].transform.position = new Vector3(newX, 0f, 0f);
}
}
}
float GetFurthestWallX()
{
float maxX = float.MinValue;
foreach (GameObject wall in walls)
{
if (wall.transform.position.x > maxX)
maxX = wall.transform.position.x;
}
return maxX;
}
}