Making a 2d side scroller car game where the ground is made out of chunks of terrain(length of camera). When a chunk leaves the screen entirely, it will destroy itself. When it is fully present in the camera, a new chunk is spawned right next to it.
However, it loads only one chunk in my entire playthrough.
Any ideas?
public class worldGen : MonoBehaviour
{
void Update()
{
var thisLand = this.gameObject; // original chunk
transform.Translate(-Vector3.right * 4 * Time.deltaTime, Space.World); // moves towards the left
if(transform.position.x + 42.75f < Camera.main.transform.position.x) // if out of sight
{
Destroy(this.gameObject);
}
if(thisLand.transform.position.x == Camera.main.transform.position.x - 0.16f) // if fully within the camera
{
Debug.Log("intantiated");
var newLand = Instantiate(this.gameObject); // instantiate new chunk
newLand.transform.position = thisLand.transform.position + new Vector3(42.75f, 0, 0);// setting the position of the chunk
Debug.Log(newLand.transform.position.x);
}
}
}