- Home /
Spawning obstacles randomly in 3D endless runner game
Hello everyone. So I managed to create an endless platform but got a problem at spawning obstacles. The problem is obstacles are not spawning around my player, they only appear and disappear in the safezone(in my TileManager script i got a safezone at 30.0f length and 15.0f width so it clons itself and destroys after couple of seconds) Here is my obstacle script
using System.Collections;
using UnityEngine;
public class GameController : MonoBehaviour
{
public GameObject obstacle;
public Vector3 spawnValues;
public int obstacleCount;
public float spawnWait;
public float startWait;
void Start()
{
StartCoroutine (SpawnObstacles());
}
void Update()
{
}
IEnumerator SpawnObstacles()
{
yield return new WaitForSeconds(startWait);
while (true)
{
for (int i = 0; i < obstacleCount; i++)
{
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, Random.Range(-spawnValues.z, spawnValues.z));
Quaternion spawnRotation = Quaternion.identity;
Instantiate(obstacle, spawnPosition, spawnRotation);
yield return new WaitForSeconds(spawnWait);
}
}
}
}
Can you help me to figure it out ?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612201639im_/https://answers.unity.com/themes/thub/images/avi.jpg)