- Home /
The question is answered, right answer was accepted
How do i make sure the spawners stay random.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class SpawnZombie : MonoBehaviour {
private Transform PlayerTransform;
public int zombAmount, WaveNumber, waveReset, WaveIncriment, waveChecker;
public GameObject[] points;
public bool reset;
public Text waveText;
int pointChoice;
private GameObject chosenSpawner;
// Start is called before the first frame update
void Start()
{
WaveIncriment = 4;
PlayerTransform = GameObject.FindGameObjectWithTag("Player").transform;
StartCoroutine(CheckRadius());
StartCoroutine(CheckWave());
reset = false;
}
void Update()
{
waveText.text = ("Wave" + WaveNumber);
}
private IEnumerator CheckRadius()
{
yield return new WaitForSeconds(0.5f);
pointChoice = Random.Range(0, points.Length);
chosenSpawner = points[pointChoice];
foreach (GameObject spawner in points)
{
if (chosenSpawner)
{
var script = chosenSpawner.GetComponent<SpawnPoint>();
if (PlayerTransform)
{
float dist = Vector3.Distance(PlayerTransform.position, chosenSpawner.transform.position);
if (dist >= 40f)
{
script.hasBegun = false;
chosenSpawner.SetActive(false);
}
if (dist < 40f)
{
chosenSpawner.SetActive(true);
if (script.hasBegun == false && zombAmount < script.waveLimiter)
{
script.StartCoroutine("SpawnZombieVoid");
}
script.hasBegun = true;
}
}
}
}
StartCoroutine(CheckRadius());
}
private IEnumerator CheckWave()
{
yield return new WaitForSeconds(2);
if(waveReset <= 0)
{
if (reset == true)
{
WaveNumber++;
WaveIncriment += 4;
reset = false;
}
}
StartCoroutine(CheckWave());
}
}
======# SECOND SCRIPT
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SpawnPoint : MonoBehaviour {
private GameObject chosenZombie;
private int zombChoice;
public GameObject[] zombiePrefab;
private SpawnZombie m_SpawnZombie;
public bool hasBegun;
public int waveLimiter;
void Start()
{
m_SpawnZombie = GetComponentInParent<SpawnZombie>();
waveLimiter = 4;
}
void Update()
{
waveLimiter = m_SpawnZombie.WaveIncriment;
}
private IEnumerator SpawnZombieVoid()
{
yield return new WaitForSeconds(3);
zombChoice = Random.Range(0, zombiePrefab.Length);
chosenZombie = zombiePrefab[zombChoice];
if (hasBegun == true)
{
if (m_SpawnZombie.zombAmount <= waveLimiter && m_SpawnZombie.waveChecker <= waveLimiter && m_SpawnZombie.zombAmount < 15)
{
GameObject zombie;
zombie = Instantiate(chosenZombie, transform.position, transform.rotation) as GameObject;
m_SpawnZombie.zombAmount++;
m_SpawnZombie.waveChecker++;
hasBegun = false;
}
if (m_SpawnZombie.waveChecker == m_SpawnZombie.waveReset)
{
hasBegun = false;
m_SpawnZombie.reset = true;
m_SpawnZombie.waveReset = 0;
m_SpawnZombie.waveChecker = 0;
}
hasBegun = false;
StartCoroutine(SpawnZombieVoid());
}
}
}
This works perfectly if I am within range of the 5 spawn points, The moment I come out of range of one of them the random factor seems to dissappear and they spawn from only the ones that the boolean hasbegun stayed on for. Now I don't want that, I need a way of checking if a spawn point has been turned off and removing it as a factor in the calculation. But the array needs to stay as it is otherwise I'm sure it won't turn back on properly once within range again.
As I said it all works fine to an extent. If I go back into range of all 5 they restart properly and the random choice returns properly. But I want the player to be able to run to another side of the map and not have zombies only coming through one window.
Thanks for any help here It's got me this one. I've.pretty. much self taught everything else but I want this to feel better than it currently does
You could probably solve this by adding another bool to keep track if spawner is on or not, if you don't want to change your script. In program$$anonymous$$g there are many ways of doing the same thing, so you could rewrite this in many different ways, some may be better.
I'll have a look at tracking another boolean in to check for the gameobjects active status, I may even throw in another small coroutine to do this alongside the others. Thanks for the idea. It's been around three days now trying different concepts.
Thanks for the advice. I worked around it by segregating the script, Creating a third coroutine, And an int tracker for how many times a spawner was used, Works perfectly now