Bool正在改变并在Unity中保持不变

时间:2015-12-15 13:40:45

标签: c# unity3d boolean coroutine

我在Unity3D中有一个相对简单的游戏,有一些僵尸向中央基地移动并攻击它直到它的健康状态达到0.攻击在共同路由中运行以在它们之间产生延迟。这里的问题是,一旦你杀死一个敌人,共同例程就会继续运行。我已经分配了一个bool isAttacking到敌人脚本,它被初始化为false,当敌人攻击时设置为true,当敌人死亡时再次设置为false。如果isAttacking设置为false,则控制攻击的协程不应该运行,但由于某种原因,即使敌人已经死亡,它仍然继续运行。真正令我感到困惑的是,如果我运行游戏,等待协程开始,杀死敌人,然后手动取消攻击' isAttacking'在检查员中,协同程序停止运行。我已经包括了Enemy& amp;基础脚本,任何帮助表示赞赏。

BASE SCRIPT

using UnityEngine;
using System.Collections;

public class Base : MonoBehaviour {

public float Health = 100f;
public float AttackSpeed = 2f;

//VOID AWAKE - START . contains getcomponent code
public Enemy enemy;
void awake(){
    enemy = GetComponent<Enemy>();
}
//VOID AWAKE - END

//If enemy touches the base 
void OnCollisionEnter2D(Collision2D col){
    Debug.Log ("Base touched");
    if(col.gameObject.tag == "Enemy"){
        enemy.isAttacking = true;
        if(Health > 0f){
            Debug.Log ("Enemy attacking base");
            StartCoroutine("enemyAttack");
        }else{
            enemy.isAttacking = false;
        }
    }
}

//Coroutine that deals damage
public IEnumerator enemyAttack(){
    while(Health > 0f){
        if(enemy.isAttacking == true){
            yield return new WaitForSeconds(2f);
            Health -= Enemy.Damage;
            Debug.Log ("Base health at: " + Health);
        }else{
            yield break;
        }
    }
}

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

    //Load Lose Screen when Base health reaches 0
    if (Health <= 0){
        Application.LoadLevel("Lose Screen");
    }
}
}

ENEMY SCRIPT

using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {

public static float Damage = 10.0f;
public float Health = 20.0f;

public Transform target;
public float Speed;

public bool isAttacking = false;

//If the player collides with the enemy
void OnTriggerEnter2D(Collider2D col)
{
    if(col.gameObject.tag == "Player")
    {
        Debug.Log("Player hits enemy");
        Health -= PlayerController.Damage;
        Debug.Log("Enemy health at: " + Health);
    }
}


// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

    //Destroy the enemy if it's health reaches 0
    if(Health <= 0){
        isAttacking = false;
        Debug.Log ("Is attacking: " + isAttacking);
        Destroy(this.gameObject);
        Debug.Log ("Enemy Destroyed!");
    }

    //Constantly move the enemy towards the centre of the gamespace (where the base is)
    float step = Speed * Time.deltaTime;
    transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}

EDIT 目前问题已解决,除了如果玩家接触基地然后所有敌人停止攻击的事实。我已更新代码以显示其当前状态。 BASE SCRIPT     使用UnityEngine;     使用System.Collections;

public class Base : MonoBehaviour {

public float Health = 100f;
public float AttackSpeed = 2f;

//VOID AWAKE - START . contains getcomponent code
public Enemy enemy;
void awake(){
    enemy = GetComponent<Enemy>();
}
//VOID AWAKE - END

//If enemy touches the base 
void OnCollisionEnter2D(Collision2D col){
    Debug.Log ("Base touched");
    if(col.gameObject.tag == "Enemy" && Health > 0f){
        enemy.isAttacking = true;
        Debug.Log ("Enemy attacking base");
        StartCoroutine("enemyAttack");
    }
    else{
        enemy.isAttacking = false;
    }
}

//Coroutine that deals damage
public IEnumerator enemyAttack(){
    while(Health > 0f){
        if(enemy.isAttacking == true){
            yield return new WaitForSeconds(2f);
            Health -= Enemy.Damage;
            Debug.Log ("Base health at: " + Health);
        }else{
            yield break;
        }
    }
}

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

    //Load Lose Screen when Base health reaches 0
    if (Health <= 0){
        Application.LoadLevel("Lose Screen");
    }

}
}

ENEMY SCRIPT

using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {

public static float Damage = 10.0f;
public float Health = 20.0f;

public Transform target;
public float Speed;

public bool isAttacking = false;

//If the player collides with the enemy
void OnTriggerEnter2D(Collider2D col)
{
    if(col.gameObject.tag == "Player")
    {
        Debug.Log("Player hits enemy");
        Health -= PlayerController.Damage;
        Debug.Log("Enemy health at: " + Health);
    }
}


// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

    //Destroy the enemy if it's health reaches 0
        if(Health <= 0){
            isAttacking = false;
            Debug.Log ("Is attacking: " + isAttacking);
            Destroy(this.gameObject);
            Debug.Log ("Enemy Destroyed!");
        }

    //Constantly move the enemy towards the centre of the gamespace (where the base is)
    float step = Speed * Time.deltaTime;
    transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}

1 个答案:

答案 0 :(得分:2)

如果添加敌人会发生什么情况.isAttacking = false;到外部的if语句?

//If enemy touches the base 
void OnCollisionEnter2D(Collision2D col){
    Debug.Log ("Base touched");
    if(col.gameObject.tag == "Enemy"){
        enemy.isAttacking = true;
        if(Health > 0f){
            Debug.Log ("Enemy attacking base");
            StartCoroutine("enemyAttack");
        }
        else{
            enemy.isAttacking = false;
        }

    }
    else{ 
        enemy.isAttacking = false;
        }
}

修改

在第一个if语句中你可以有

if(col.gameObject.tag == "Enemy" && Health > 0f){
...

然后也许甚至没有内在的if else?

修改

对于不死的僵尸使用像

这样的while循环
    // Update is called once per frame
void Update () 
{
    while(isAttacking == true)
    {
        if(Health <= 0)
        {
            Debug.Log ("Is attacking: " + isAttacking);
            Destroy(this.gameObject);
            Debug.Log ("Enemy Destroyed!");
            isAttacking = false;
        }

        //Constantly move the enemy towards the centre of the gamespace (where the base is)
        float step = Speed * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position, target.position, step);
    }
}