碰撞阻止敌人进攻 - Unity3D

时间:2015-12-15 16:09:06

标签: c# unity3d boolean collision

我有一个简单的游戏,玩家可以防御中心基地攻击敌人。目前,当玩家触及基地时,敌人全部停止攻击。攻击由敌人脚本中的共同例程处理。我在下面发布了敌人和基础脚本。 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);
}
}

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

}
}

1 个答案:

答案 0 :(得分:0)

我怀疑你的问题与这里的逻辑有关:

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

不,你的条件是:

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

因此,如果事物与基地发生碰撞(如果我正确理解这一点)不是“敌人”,那么你进入else子句:

enemy.isAttacking = false;

阻止敌人进攻。

你想要的可能是:

if (col.gameObject.tag == "Enemy")
{
    if (Health > 0f) 
    {
        //...do stuff
    }
    else
    {
        enemy.isAttacking = false;
    }
}

现在不是敌人接触你的基地的东西不会停止影响敌人。

但很难说你何时在很少或没有解释的情况下将代码转储,所以这可能会有所不同。