计时器结束时如何将玩家带到失败的场景,并且不会杀死足够多的敌人

时间:2019-05-10 12:30:53

标签: c# visual-studio unity3d

我正在创建一个游戏,玩家需要在计时器用尽之前杀死特定数量的敌人。如果玩家没有杀死他打算杀死的敌人数量,并且计时器耗尽,他将被带到失败现场。我的问题是,即使计时器用尽并且不能杀死所需数量的敌人,他也不会被带到失败现场。我该如何解决?

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;
int EnemiesToNextLevel = 7;
int KilledEnemies = 0;
public LevelManager myLevelManager;
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 < 5)
     {
         // You killed everyone, change scene: 
         LaserLevelManager.LoadLevel("NextLevelMenu");
     }
     */

   /* KilledEnemies++;
    if (KilledEnemies >= EnemiesToNextLevel)
    {
        LaserLevelManager.LoadLevel("NextLevelMenu");
    }
    */

    if(Time.timer == 0.0f && KilledEnemies != EnemiesToNextLevel)
    {
        LaserLevelManager.LoadLevel("Lose");
    }


    }



}

计时器脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Timer : MonoBehaviour
 {
public Text TimerText;
public float MainTime;
public static float timer;
public  bool CanCount = true;
public  bool OnlyOnce = false;

void Start()
{
    timer = MainTime; //Timer is gonna be equal to what we set MainTime in the inspector 
}

void Update()
{
    if (timer >= 0.0f && CanCount)
    {
        timer -= Time.deltaTime;//The timer will count down in delta time based on the seconds we have 
        TimerText.text = timer.ToString("F");//Converting Timer into a string value 
    }

    //This means that when the timer reaches zero and OnlyOnce is equal to false 
    else if (timer <= 0.0f && !OnlyOnce) //If the timer goes below 0 and OnlyOnce is not equal to flase 
    {

        CanCount = false; //CanCount is equal to false so we are not counting down anymore since it's not greater than zero and we can't count anymore
        OnlyOnce = true; //This is goning to be done once when it reaches it 
        TimerText.text = "0.00"; //Setting the UI to 0 
        timer = 0.0f; //Setting the timer to 0
    }

    }
}

2 个答案:

答案 0 :(得分:1)

使用当前代码结构:您的敌人生成者可以将EnemyKilled和EnemiesToNextLevel变量作为公共变量。并且,您可以在计时器脚本中使用EnemySpawner的Instance变量。

New EnemySpawner.cs

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 int EnemiesToNextLevel = 7;
public int KilledEnemies = 0;
public LevelManager myLevelManager;
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 < 5)
 {
     // You killed everyone, change scene: 
     LaserLevelManager.LoadLevel("NextLevelMenu");
 }
 */
KilledEnemies++;

if (Time.timer > 0 && KilledEnemies >= EnemiesToNextLevel)
{
    LaserLevelManager.LoadLevel("NextLevelMenu");
}   
}
}

修改后的计时器脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Timer : MonoBehaviour
 {
public Text TimerText;
public float MainTime;
public static float timer;
public  bool CanCount = true;
public  bool OnlyOnce = false;

void Start()
{
timer = MainTime; //Timer is gonna be equal to what we set MainTime in the     inspector 
}

void Update()
{
if (timer >= 0.0f && CanCount)
{
    timer -= Time.deltaTime;//The timer will count down in delta time based on the seconds we have 
    TimerText.text = timer.ToString("F");//Converting Timer into a string value 
}

//This means that when the timer reaches zero and OnlyOnce is equal to false 
else if (timer <= 0.0f && !OnlyOnce) //If the timer goes below 0 and OnlyOnce is not equal to flase 
{

    CanCount = false; //CanCount is equal to false so we are not counting down anymore since it's not greater than zero and we can't count anymore
    OnlyOnce = true; //This is goning to be done once when it reaches it 
    TimerText.text = "0.00"; //Setting the UI to 0 
    timer = 0.0f; //Setting the timer to 0
    if(Time.timer == 0.0f && EnemySpawner.Instance.KilledEnemies != EnemySpawner.Instance.EnemiesToNextLevel)
    {
    LaserLevelManager.LoadLevel("Lose");
    }
        }

    }
}

PS:可以改进代码。让我知道是否有帮助。

答案 1 :(得分:0)

在计时器的更新功能中

void Update()
{
if (timer >= 0.0f && CanCount)
{
    timer -= Time.deltaTime;//The timer will count down in delta time based on the seconds we have 
    TimerText.text = timer.ToString("F");//Converting Timer into a string value 
}

//This means that when the timer reaches zero and OnlyOnce is equal to false 
else if (timer <= 0.0f && !OnlyOnce) //If the timer goes below 0 and OnlyOnce is not equal to flase 
{

    CanCount = false; //CanCount is equal to false so we are not counting down anymore since it's not greater than zero and we can't count anymore
    OnlyOnce = true; //This is goning to be done once when it reaches it 
    TimerText.text = "0.00"; //Setting the UI to 0 
    timer = 0.0f; //Setting the timer to 0
    EnemySpawner.timeIsZero = true;
}

}
}

然后在您的EnemySpawner脚本中添加一个公共的静态布尔timeIsZero = false;。在顶部,然后在

public void OnEnemyDeath()
 {
   /*  CurrentNumOfEnemies--;
     if (CurrentNumOfEnemies < 5)
     {
         // You killed everyone, change scene: 
         LaserLevelManager.LoadLevel("NextLevelMenu");
     }
     */

   /* KilledEnemies++;
    if (KilledEnemies >= EnemiesToNextLevel)
    {
        LaserLevelManager.LoadLevel("NextLevelMenu");
    }
    */
if(timeIsZero && KilledEnemies != EnemiesToNextLevel)
{
    LaserLevelManager.LoadLevel("Lose");
}


}



}