布尔正在改变,并在团结中保持不变

本文关键字:改变 布尔正 | 更新日期: 2023-09-27 18:27:02

我在Unity3D中有一个相对简单的游戏,一些僵尸向中心基地移动并攻击它,直到它的生命值达到0。攻击以共同路由方式运行,以在它们之间产生延迟。这里的问题是,一旦你杀死了一个敌人,协同程序就会继续运行。我为Enemy脚本分配了一个bool isAttacking,该脚本初始化为false,在敌人进攻时设置为true,在敌人死亡时再次设置为false。如果isAttacking设置为false,那么控制攻击的协同程序就不应该运行,但由于某种原因,即使敌人已经死亡,它仍然会继续运行。真正让我困惑的是,如果我运行游戏,等待协同程序开始,杀死敌人,然后在检查器中手动取消勾选"正在攻击",那么协同程序就会停止运行。我把敌人和;基本脚本,任何帮助都将不胜感激。

基本脚本

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");
    }
}
}

敌方脚本

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

编辑目前这个问题已经解决了,只是如果玩家碰到基地,所有敌人都会停止攻击。我已经更新了代码以显示它的当前状态。基本脚本使用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");
    }
}
}

敌方脚本

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

布尔正在改变,并在团结中保持不变

如果添加一个enemy.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){
...

那也许连内心都没有,如果有的话?

编辑

对于没有死亡的僵尸使用像一样的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);
    }
}