Hello everyone!
I am trying to make a basic 3d game, where I want to randomly spawn obstacles in front of the player…
There is an area (Block), and onto that area would be the player and in front of him would be obstacles, which would be randomly spawned using script. I made script, but then it spawns everytime on the same place…
Please help, I really don’t know how to fix it…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObstacleSpawner : MonoBehaviour {
public Transform[] spawnPoints;
public GameObject obstaclePrefab;
public float timeBetweenWaves = 1f;
private float timeToSpawn = 2f;
public float test = 15f;
void Update ()
{
if (Time.time >= timeToSpawn)
{
SpawnObstacles();
timeToSpawn = Time.time + timeBetweenWaves;
}
}
void SpawnObstacles ()
{
int randomIndex = Random.Range(0, spawnPoints.Length);
for (int i = 0; i < spawnPoints.Length; i++)
{
if (randomIndex == i)
{
Instantiate(obstaclePrefab, spawnPoints*.position, Quaternion.identity);*
}
}
}
}
,