Add spawners

This commit is contained in:
Jens Pitkänen 2019-08-04 21:52:07 +03:00
parent 0f42de39e1
commit b872cf3f5a
4 changed files with 48 additions and 1 deletions

View File

@ -24,7 +24,7 @@ GameObject:
- component: {fileID: 592487110401094205} - component: {fileID: 592487110401094205}
m_Layer: 0 m_Layer: 0
m_Name: Enemy (Default Melee) m_Name: Enemy (Default Melee)
m_TagString: Untagged m_TagString: Enemy
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0

View File

@ -0,0 +1,35 @@
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);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3adf9e5649b7c874f9f5ef2a5d70f99a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -5,6 +5,7 @@ TagManager:
serializedVersion: 2 serializedVersion: 2
tags: tags:
- Environment - Environment
- Enemy
layers: layers:
- Default - Default
- TransparentFX - TransparentFX