【问题标题】:Load the scene when all enemies die当所有敌人死亡时加载场景
【发布时间】:2019-05-09 04:27:44
【问题描述】:

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


}

EnemyShooting 脚本:

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();

    }
  }

【问题讨论】:

  • 您可以将 currentNumberOfEnemies 更改为 numberOfEnemiesForLevel,将其实例化为该级别所需的敌人数量(即此级别为 5),然后从生成脚本中删除增量。

标签: c# visual-studio unity3d


【解决方案1】:

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

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

【讨论】:

  • 为了确定,所以在 OnEnemyDeath() 我添加 KilledEnemies++;并在同一个函数中添加 if(KilledEnemies>=EnemiesToNextLevel){ LaserLevelManager.LoadLevel("NextLevelMenu")}
  • 是的,这就行了。然后对于每个级别,您需要将 EnemiesToNextLevel 设置为给定值并将 KilledEnemies 设置为 0。
  • 所以对于 2 级我将有 10 个敌人我设置 Enemiestonextlevel =10;和杀死敌人 = 0;
  • 在对响应添加检查后,您已经拥有的那个没有用。
猜你喜欢
  • 2021-03-24
  • 2022-10-08
  • 2022-12-18
  • 2020-08-18
  • 1970-01-01
  • 2019-11-29
  • 1970-01-01
  • 2016-03-09
  • 1970-01-01
相关资源
最近更新 更多