首页 > 解决方案 > 当所有敌人死亡时加载场景

问题描述

我正在统一创建一个激光防御游戏,我遇到了这个问题,当我射击第一个敌人时,它会将我直接带到 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();

    }
  }

标签: c#visual-studiounity3d

解决方案


我会在 spawner 脚本中添加两个字段。EnemiesToNextLevel 和 KilledEnemies。然后在你的 spawner 的 OnEnemyDeath() 中,你可以在每次调用它时增加 KilledEnemies,然后在改变场景之前询问 KilledEnemies >= EnemiesToNextLevel。

当然还有很多其他方法可以做到这一点,但对我来说这是最简单的。


推荐阅读