c# - 当所有敌人死亡时加载场景
问题描述
我正在统一创建一个激光防御游戏,我遇到了这个问题,当我射击第一个敌人时,它会将我直接带到 NextLevelMenu 场景,但我希望它在所有敌人都被杀死时加载(在这个级别上我有 5 个敌人杀)。有人告诉我,我需要向每个生成的敌人发送对其生成器实例的引用,但我不太明白。有人可以帮忙吗?
EnemySpawner 脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnemySpawner : MonoBehaviour {
[SerializeField] GameObject EnemyPreFab;
[SerializeField] int MaxEnemies = 30;
[SerializeField] float EnemySpawnTime = 1.00001f;
[SerializeField] GameObject FirstWaypoint;
int CurrentNumOfEnemies = 0;
public LevelManager myLevelManager;
public int maximumnumberofhits = 0;
public static EnemySpawner Instance = null;
int timesEnemyHit;
IEnumerator SpawningEnemies()
{
while(CurrentNumOfEnemies <= MaxEnemies)
{
GameObject Enemy = Instantiate(EnemyPreFab, this.transform.position, Quaternion.identity);
CurrentNumOfEnemies++;
yield return new WaitForSeconds(EnemySpawnTime);
}
}
void Start()
{
if (Instance == null)
Instance = this;
StartCoroutine(SpawningEnemies());
timesEnemyHit = 0;
if (this.gameObject.tag == "EnemyHit")
{
CurrentNumOfEnemies++;
}
}
public void OnEnemyDeath()
{
CurrentNumOfEnemies--;
if (CurrentNumOfEnemies < 1)
{
// You killed everyone, change scene:
LaserLevelManager.LoadLevel("NextLevelMenu");
}
}
}
敌人射击脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyShooting : MonoBehaviour {
[SerializeField] float EnemyLaserSpeed = 10f;
[SerializeField] float EnemyLaserFireTime;
[SerializeField] GameObject LaserBulletEnemyPreFab;
[SerializeField] int MaxNumberOfHits = 1;
int CurrentNumberOfHits = 0;
Coroutine FireCoroutine;
void OnTriggerEnter2D(Collider2D collider)
{
if(collider.gameObject.tag == "PlayerLaser")
{
if(CurrentNumberOfHits < MaxNumberOfHits)
{
CurrentNumberOfHits++;
Destroy(collider.gameObject);
Score.ScoreValue += 2;//The user will be rewarded 1 point
}
}
}
void DestroyEnemy()
{
if(CurrentNumberOfHits >= MaxNumberOfHits)
{
Destroy(gameObject);
EnemySpawner.Instance.OnEnemyDeath(); // Tell the EnemySpawner that someone died
}
}
private void Fire()
{
FireCoroutine = StartCoroutine(ShootContinuously());
}
void BecomeVisible()
{
Fire();
}
IEnumerator ShootContinuously()
{
while (true)
{
GameObject LaserBulletEnemy = Instantiate(LaserBulletEnemyPreFab, this.transform.position, Quaternion.identity) as GameObject;
LaserBulletEnemy.GetComponent<Rigidbody2D>().velocity = new Vector2(0, EnemyLaserSpeed);
EnemyLaserFireTime = Random.Range(0.5f, 0.9f);
yield return new WaitForSeconds(EnemyLaserFireTime);
}
}
// Use this for initialization
void Start () {
BecomeVisible();
}
// Update is called once per frame
void Update () {
DestroyEnemy();
}
}
解决方案
我会在 spawner 脚本中添加两个字段。EnemiesToNextLevel 和 KilledEnemies。然后在你的 spawner 的 OnEnemyDeath() 中,你可以在每次调用它时增加 KilledEnemies,然后在改变场景之前询问 KilledEnemies >= EnemiesToNextLevel。
当然还有很多其他方法可以做到这一点,但对我来说这是最简单的。
推荐阅读
- java - 带有嵌入式 Hazlecast 的 Quarkus 在 AWS 上失败并出现错误“无法配置发现策略”
- javascript - 我该如何解决这个“关键”警告?
- typescript - 如何键入对 Typescript 对象的链式调用
- java - 在 Java ME 中将 UTF-16BE 转换为 UTF-8
- vue.js - Symfony Encore + Vue.js:“无法安装组件:未定义模板或渲染函数”
- azure - Azure Policy 验证是否未选择 TLS 字段
- sql - 我可以使用delphi编辑作为名称在sql中创建表吗
- css - CSS:如何修复这样的符号的多余空间(当 line-height 不削减它时)
- types - 为什么 IntoIter 不拥有这些值?
- ios - Swift 等效于每次按下“x”按钮以执行操作