Question by
Rajan95 · Oct 28, 2019 at 09:38 PM ·
instantiatespawnspawning problems
How do I spawn a certain amount of objects randomly between 2 positions?
I'm having trouble with implementing a wave system in my game.
Here's what I'm trying to do:
1) Set an amount for each object to spawn within the wave i.e 1 enemy and 5 objects
2) I'm instantiating from two positions which has different rotations for each, is there a way to check to see if object is spawning from left position then apply rotation x
3) I'm super new to programming so any help with this will be really appreciated!
public class spawnManager2 : MonoBehaviour
{
public GameObject [] randomSpawn;
public int spawnIndex;
public GameObject[] bfPrefab;
public int bfIndex;
private int[] spawnRotations = {-20, 20};
private int rotationIndex;
private playerController playerScript;
public int bombCount;
public int fruitCount;
private int fruit;
private int bomb;
// Start is called before the first frame update
void Start()
{
randomSpawn[0] = GameObject.FindGameObjectWithTag("Left Spawn");
randomSpawn[1] = GameObject.FindGameObjectWithTag("Right Spawn");
StartCoroutine(stage1());
}
// Update is called once per frame
void Update()
{
fruit = GameObject.FindGameObjectsWithTag("Fruit").Length;
bomb = GameObject.FindGameObjectsWithTag("Bomb").Length;
Debug.Log("Fruit count: " + fruit + " Bomb count: " + bomb);
}
IEnumerator stage1()
{
yield return new WaitForSeconds(1);
for (int i = 0; fruit < fruitCount; i++)
{
yield return new WaitForSeconds(1);
spawnLeft();
fruit++;
}
for (int i = 0; bomb < bombCount; i++)
{
yield return new WaitForSeconds(1);
spawnRight();
bomb++;
}
}
void spawnLeft()
{
bfIndex = Random.Range(0, bfPrefab.Length);
spawnIndex = Random.Range(0, randomSpawn.Length);
Instantiate(bfPrefab[1], randomSpawn[0].transform.position, bfPrefab[bfIndex].transform.rotation * Quaternion.Euler(0f, 0f, spawnRotations[0]));
}
void spawnRight()
{
bfIndex = Random.Range(0, bfPrefab.Length);
spawnIndex = Random.Range(0, randomSpawn.Length);
Instantiate(bfPrefab[0], randomSpawn[1].transform.position, bfPrefab[bfIndex].transform.rotation * Quaternion.Euler(0f, 0f, spawnRotations[1]));
}
day-33.gif
(19.0 kB)
Comment