69 lines
1.3 KiB
C#
69 lines
1.3 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
public class EnemySpawner : MonoBehaviour
|
|
{
|
|
[Header("Enemy")]
|
|
[SerializeField]
|
|
private GameObject enemyPrefab;
|
|
|
|
[Header("Spawn")]
|
|
[SerializeField]
|
|
private Transform spawnPoint;
|
|
|
|
[SerializeField]
|
|
private float minSpawnDelay = 0.5f;
|
|
|
|
[SerializeField]
|
|
private float maxSpawnDelay = 2f;
|
|
|
|
private void Start()
|
|
{
|
|
if (enemyPrefab == null)
|
|
{
|
|
Debug.LogError(
|
|
"EnemySpawner: Kein Enemy-Prefab zugewiesen!",
|
|
this
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
if (spawnPoint == null)
|
|
{
|
|
Debug.LogError(
|
|
"EnemySpawner: Kein SpawnPoint zugewiesen!",
|
|
this
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
StartCoroutine(SpawnLoop());
|
|
}
|
|
|
|
private IEnumerator SpawnLoop()
|
|
{
|
|
while (true)
|
|
{
|
|
float delay = UnityEngine.Random.Range(
|
|
minSpawnDelay,
|
|
maxSpawnDelay
|
|
);
|
|
|
|
yield return new WaitForSeconds(delay);
|
|
|
|
SpawnEnemy();
|
|
}
|
|
}
|
|
|
|
private void SpawnEnemy()
|
|
{
|
|
Instantiate(
|
|
enemyPrefab,
|
|
spawnPoint.position,
|
|
spawnPoint.rotation
|
|
);
|
|
}
|
|
}
|