Hi all! Here’s my problem.
I’m working on little simple isometric game: you are a tank inside an arena and enemy spawn in random position, then they start to chase you. If they touch you, you lose. You gotta shoot em to survive as long as possibile.
Here’s the problem: I noticed that sometimes the enemy spawn randomly on the player position, creating a situation where death is inevitable, and that’s quite frustrating. I was wondering if anyone could help me improving my script to avoid this situation. My hypothesis was to create a little collider around the player where nothing could spawn inside, but where enemy could move freely to get you, but i’m sure that someone more expert than me would find a better solution. Here’s what i managed to do until now:
using UnityEngine;
using System.Collections;
public class EnemySpawn : MonoBehaviour
{
public GameObject enemy;
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
void Start ()
{
StartCoroutine (SpawnWaves ());
}
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds (startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, Random.Range (-spawnValues.z, spawnValues.z));
Quaternion spawnRotation = Quaternion.identity;
Instantiate (enemy, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}
yield return new WaitForSeconds (waveWait);
}
}
}
Hope I was clear enough. For any other info, just ask. Thank you all!