hello, my friends I have a game where my world will spawn no matter where the player go,
after a while, it stops spawning
the way I make the world infinite 3 thing
1- level template in the template has 4 spawn point
2- each spawn point will spawn the same template, so the world will continue spawning in all directions, after checking if there is already spawned level or not
3- spawn object, will spawn the obstacles, etc
the problem, sometime after the spawn point disappears nothing spawns in its place and the world stops spawning like this, as far as I try it random time until it breaks, but after it happens it will happen fast after restarting the game, ex: the first time it takes 10 min, second time will take time 1 min, this will keep happening until I close the unity and re-open it, I don’t have any const variable
also if like the down point stops appearing, the other level above it and under it, have the same problem the down point stops spawning same for the left and right
this is the code I use :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnerManger : MonoBehaviour
{
[SerializeField] private GameObject level;
[SerializeField] private GameObject destroy_GameObject;
[SerializeField] private LayerMask layermask;
private BoxCollider2D point_colider;
private GameObject player_gameObject;
private Transform player;
private float Player_distance;
private void Awake()
{
}
private void Start()
{
player_gameObject = GameObject.FindGameObjectWithTag("Player_");
player = player_gameObject.transform;
GetComponent<BoxCollider2D>().enabled= true;
}
private void Update()
{
// check the distance between player and spawn point
Player_distance = Vector2.Distance(transform.position, player.transform.position);
Debug.Log(Player_distance);
if (Player_distance <= 30)
{
spawn_level();
}
}
////spawn a level if there is non in the place
private void spawn_level()
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.up, 1f , layermask );
if (hit.collider == null)
{
Instantiate(level, transform.position, Quaternion.identity);
StartCoroutine(destroy_point());
}
else if (hit.collider.tag == "Earth")
{
return;
}
}
private IEnumerator destroy_point()
{
yield return new WaitForSeconds(1);
Destroy(destroy_GameObject);
}
}
also if my way to make the infinite world is wrong or has problems and there is a better way to make the world infinite I will be happy to know it