BloodAndGore/Assets/Scripts/Enemies/EnemySpawner.cs

36 lines
1.4 KiB
C#
Raw Permalink Normal View History

2019-08-04 20:52:07 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2019-08-14 16:17:48 +02:00
namespace Saltosion.OneWeapon.Enemies {
2019-08-04 20:52:07 +02:00
public class EnemySpawner : MonoBehaviour {
public GameObject[] EnemyPrefabs;
public float SpawnCooldown = 5.0f;
public float SpawnChance = 0.5f;
public int MaxEnemiesWhenSpawning = 5;
public float Radius = 4;
private float SpawnTime = 0.0f;
private void Update() {
if (SpawnTime > 0) {
SpawnTime -= Time.deltaTime;
} else {
GameObject[] Enemies = GameObject.FindGameObjectsWithTag("Enemy");
if (Enemies.Length < 5 && Random.value < SpawnChance) {
float DirectionRadians = Random.value * Mathf.PI * 2.0f;
Vector2 Direction = new Vector2(Mathf.Cos(DirectionRadians), Mathf.Sin(DirectionRadians));
float Distance = Random.value * Radius;
Instantiate(EnemyPrefabs[(int)(Mathf.Min(0.99f, Random.value) * EnemyPrefabs.Length)], (Vector2)transform.position + Direction * Distance, new Quaternion(), null);
}
SpawnTime = SpawnCooldown;
}
}
private void OnDrawGizmosSelected() {
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(transform.position, Radius);
}
}
}