So, in the above images you can see my script generates platform though I want to limit the generation to only main camera s width i.e. the generated platform should not go beyond camera screen width. If you had played the available game you will know.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PathGenerator : MonoBehaviour
{
private Vector3 spawnPos;
[SerializeField] Vector3 lastPos;
[SerializeField] float offset;
[SerializeField] GameObject trackPrefab;
private GameObject temp;
[SerializeField] int amountToPool;
List<GameObject> platformPool = new List<GameObject>();
// Start is called before the first frame update
void Start()
{
CreatePath(amountToPool);
UsePath();
}
// Update is called once per frame
void Update()
{
}
private void CreatePath(int amt)
{
for (int i = 0; i < amt; i++)
{
temp = Instantiate(trackPrefab);
temp.transform.SetParent(GameObject.Find("Tracks").transform);
platformPool.Add(temp);
}
}
private void UsePath()
{
foreach (GameObject p in platformPool)
{
RandomPicker();
p.transform.position = spawnPos;
lastPos = p.transform.position;
}
}
public void RespawnTrack(GameObject pathGameObject)
{
RandomPicker();
pathGameObject.transform.position = spawnPos;
lastPos = pathGameObject.transform.position;
}
private void RandomPicker()
{
var randomNumber = Random.Range(0, 2);
if (randomNumber < 1)
{
spawnPos = new Vector3(lastPos.x - offset, 0, lastPos.z);
}
else
{
spawnPos = new Vector3(lastPos.x, 0, lastPos.z - offset);
}
lastPos = spawnPos;
}
}
I want to move my cam exactly and also you see how the platforms are never off screen.