BloodAndGore/Assets/Scripts/EnemySpawner.cs

36 lines
1.3 KiB
C#
Raw Normal View History

2019-08-04 20:52:07 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Saltosion.OneWeapon {
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);
}
}
}