|
LivingEntity에서 아래의 코드를 잘 삽입~
public event System.Action OnDeath;
if (OnDeath != null)
{
OnDeath(); //죽을때마다. 이벤트가 들어감
}
Spawner.cs
- using UnityEngine;
- using System.Collections;
- public class Spawner : MonoBehaviour {
- public Wave[] waves;
- public Enemy enemy;
- int enemiesRemainingToSpawn;
- int enemiesRemainingAlive;
- float nextSpawnTime;
- Wave currentWave;
- int currentWaveNumber;
- void Start()
- {
- NextWave();
- }
- void Update()
- {
- if (enemiesRemainingToSpawn > 0 && Time.time > nextSpawnTime)
- {
- enemiesRemainingToSpawn--;
- nextSpawnTime = Time.time + currentWave.timeBetweenSpawns;
- Enemy spawnedEnemy = Instantiate(enemy, Vector3.zero,
- Quaternion.identity)as Enemy;
- spawnedEnemy.OnDeath += OnEnemyDeath;
- }
- }
- void OnEnemyDeath()
- {
- enemiesRemainingAlive--;
- if (enemiesRemainingAlive == 0)
- {
- NextWave();
- }
- }
- void NextWave()
- {
- currentWaveNumber++;
- print("Wave:" + currentWaveNumber);
- if(currentWaveNumber - 1 < waves.Length)
- {
- currentWave = waves[currentWaveNumber - 1];
- enemiesRemainingToSpawn = currentWave.enemyCount;
- enemiesRemainingAlive = enemiesRemainingToSpawn;
- }
- }
- [System.Serializable]
- public class Wave
- {
- public int enemyCount;
- public float timeBetweenSpawns;
- }
- }
반응형
'응용프로그램 > 유니티(Unity)' 카테고리의 다른 글
Unity - 적 공격으로 Player에게 데미지 입히기 (0) | 2016.11.17 |
---|---|
unity - 대칭함수을 활용한 공격 (0) | 2016.11.16 |
Unity - 데미지 시스템 (0) | 2016.11.14 |
Unity - 충돌감지관련 (0) | 2016.11.14 |
Unity - CapsuleCollider 사이의 거리알아보기. (0) | 2016.11.13 |