- Home /
Question by
mitendewking · May 29, 2015 at 05:33 AM ·
randomspawnspawning problemsenemieswaves
How to create random spawn for an object? (C#)
i need to spawn a prefab i created ( tagged "enemy") across carious random points on the board, (which spans from -20 to 20 on both the x and z axis) I also would like it to spawn in waves so if possible keep the base formatting of the code. code is as follows:
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour {
public GameObject hazard;
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, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}
yield return new WaitForSeconds (waveWait);
}
}
}
Comment
You didn't say if there is any problem with the code you posted! What is the question here?
Format your code using the 101010 button, and ask a question. Unity Answers isn't a script writing service.