Raycasts停止检测碰撞器(或碰撞器停止检测光线投射)

时间:2015-05-12 17:49:13

标签: c# unity3d raycasting

我一直致力于基于团队的基本AI射击系统,其中机器人可以在机器人身上射击人类和人类。该系统有效,两支队伍都可以射击并互相杀戮,但是一旦被杀,它的射线扫描似乎无法探测到敌人的对手。这样做的结果是杀死另一个的机器人不再被该机器人损坏,因此可以"阵营"它何时重生。

编辑:当他们重生时,他们仍然正常进行光线投射,它只是没有检测到与对手的碰撞

我的拍摄完全是通过光线投射完成的并且杀死/重生我的机器人我将它们停用并将它们移回它们的产卵位置。跟踪其他团队是通过NavMeshAgents和agent.destination完成的。以下是适合他们的所有脚本。对不起,如果它很多。

AndroidShoot脚本

using UnityEngine;
using System.Collections;

public class AndroidShoot : MonoBehaviour {

public GameObject humanTarget;
public GameObject playerTarget;
public PlayerHealth playerHealth;
public allyHealth allyHealth;

public lastShooter lastShot;

float gunDelay = 0.75f;
bool isShooting = false;

// Update is called once per frame
void Update () {
    shootGun ();
}

// Name what for the raycast collides with (used to reference the target point)
RaycastHit hit;
void shootGun() {

    Vector3 rayDirection = GetComponent<Rigidbody>().transform.forward;

    if (Physics.Raycast (transform.position + transform.up * 1.5f, rayDirection, out hit)) {
        Debug.Log("enemy shoot check");
        Debug.DrawLine(transform.position + transform.up * 1.5f, hit.point, Color.yellow, 2f);

HumanShoot脚本

using UnityEngine;
using System.Collections;

public class humanShoot : MonoBehaviour {

public mainEnemyLife enemyHealth;
public lastShooter lastShot;

float gunDelay = 0.75f;
bool isShooting = false;

// Update is called once per frame
void Update () {
    shootGun ();
}

RaycastHit hit;
void shootGun() {

    Vector3 rayDirection = GetComponent<Rigidbody>().transform.forward;

    if (Physics.Raycast (transform.position + transform.up * 1.5f, rayDirection, out hit)) {
        Debug.Log("ally shoot check");
        Debug.DrawLine(transform.position + transform.up * 1.5f, hit.point, Color.black, 2f);

        if(hit.collider.gameObject.CompareTag ("Enemy_Torso") && isShooting == false) {
            isShooting = true;
            StartCoroutine (enemyShootAlly ());
            Debug.DrawLine(transform.position + transform.up * 1.5f, hit.point, Color.cyan, 2f);
            lastShot.shotLastUpdate(this.gameObject.name);
        }
        if(hit.collider.gameObject.CompareTag ("Enemy_Head") && isShooting == false) {
            isShooting = true;
            StartCoroutine (enemyShootAlly ());
            Debug.DrawLine(transform.position + transform.up * 1.5f, hit.point, Color.cyan, 2f);
            lastShot.shotLastUpdate(this.gameObject.name);
        }
        if(hit.collider.gameObject.CompareTag ("Enemy_Limb") && isShooting == false) {
            isShooting = true;
            StartCoroutine (enemyShootAlly ());
            Debug.DrawLine(transform.position + transform.up * 1.5f, hit.point, Color.cyan, 2f);
            lastShot.shotLastUpdate(this.gameObject.name);
        }
    }
}

IEnumerator enemyShootAlly() {
    Debug.Log("enemy damaged");

    enemyHealth.enemyHealth -= 15f;

    yield return new WaitForSeconds (gunDelay);
    Debug.Log ("ally can shoot");
    isShooting = false;
}
}

AndroidHealth脚本

using UnityEngine;
using System.Collections;

public class mainEnemyLife : MonoBehaviour {

public float enemyHealth = 100.0f;

public lastShooter lastShooter;
public enemyRespawn enemyRespawn;
public ScoreboardDisplay scoreboardScore;

// Update is called once per frame
void Update () {
    enemyDeath();
}

public void enemyTakeDamage(float damage) {
    enemyHealth -= damage;
}

public void enemyDeath() {
    if (enemyHealth <= 0.0f) {
        Debug.Log(this.gameObject.name + " was killed by " + lastShooter.shotLast);
        scoreboardScore.scoreboardIncrease();
        enemyRespawn.respawnEnemy();
        enemyHealth = 100.0f;
    }
}
}

HumanHealth脚本

using UnityEngine;
using System.Collections;

public class allyHealth : MonoBehaviour {

public float humanHealth = 100.0f;
public string allyName;

public allyRespawn humanRespawn;
public lastShooter lastShooter;
public ScoreboardDisplay scoreboardScore;

// Use this for initialization
void Start () {
    allyName = this.gameObject.name;
}
// Update is called once per frame
void Update () {
    humanDeath ();
}
void humanDeath() {
    if (humanHealth <= 0f) {
        gameObject.SetActive(false);
        Debug.Log(allyName + " was killed by " + lastShooter.shotLast);
        scoreboardScore.scoreboardIncrease();
        humanRespawn.respawnAlly();
        humanHealth = 100.0f;
    }
}
}

AndroidRespawn脚本

using UnityEngine;
using System.Collections;

public class enemyRespawn : MonoBehaviour {

public float respawnTime = 1f;
public GameObject enemy;

public void respawnEnemy() {
    StartCoroutine (androidRespawn());
}

IEnumerator androidRespawn() {
    enemy.SetActive (false);
    yield return new WaitForSeconds (respawnTime);
    enemy.transform.position = this.transform.position;
    enemy.SetActive (true);
    Debug.Log (enemy.name + " has respawned.");
}
}

HumanRespawn脚本

using UnityEngine;
using System.Collections;

public class allyRespawn : MonoBehaviour {

public float respawnTime = 1f;
public GameObject ally;

public void respawnAlly() {
    StartCoroutine (humanRespawn());
}

IEnumerator humanRespawn() {
    ally.SetActive(false);
    yield return new WaitForSeconds (respawnTime);
    ally.transform.position = this.transform.position;
    Debug.Log (ally.name + " has respawned.");
    ally.SetActive (true);
}
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

由于rutter找到了解决方案:

禁用游戏对象或组件会停止附加到它的协同程序,并且重生的僵尸程序被卡在isShooting = true上,因此无法触发。我通过设置isShooting = false来修复它;在respawn函数中。