Question by
Darmikecnho · Sep 07, 2020 at 05:11 PM ·
c#unity 2dspawning
I need help making my objects spawn correctly.
Hello I am new to all of this but I am trying to make a game where you move left and right to dodge obstacles, I am having trouble with making them spawn how I want them to. I am trying to get them to spawn at random times in a predetermined spot, I also only want three of them on screen at a time but once one gets destroyed it can spawn another one at one of the spawn points.
Here is my code so far. I am kinda stuck.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawning: MonoBehaviour
{
[SerializeField] GameObject UpEnemy;
[SerializeField] GameObject DownEnemy;
[SerializeField] float Spawndelay;
[SerializeField] float Spawntime;
Vector2 Spawner1 = new Vector2(-3, 9);
Vector2 Spawner2 = new Vector2(0, -9);
Vector2 Spawner3 = new Vector2(3, 9);
Quaternion NoRotation = Quaternion.identity;
Quaternion SomeRotation = Quaternion.Euler(0, 0, 180);
public int EnemyCount = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (EnemyCount <= 2)
{
StartCoroutine(EnemySpawn());
}
if (EnemyCount >= 3)
{
StopCoroutine(EnemySpawn());
}
}
private IEnumerator EnemySpawn()
{
WaitForSeconds wait = new WaitForSeconds(Spawndelay);
yield return new WaitForSeconds(Spawntime);
for (int count = EnemyCount; count > 0; --count)
{
Instantiate(UpEnemy, Spawner2, NoRotation);
EnemyCount++;
Instantiate(DownEnemy, Spawner1, SomeRotation);
EnemyCount++;
Instantiate(DownEnemy, Spawner3, SomeRotation);
EnemyCount++;
yield return wait;
}
Debug.Log("All the Cars are Vrooming");
}
}
Comment