簡體   English   中英

射線廣播停止檢測對撞機(或對撞機停止檢測對撞機)

[英]Raycasts stop detecting colliders (or colliders stop detecting raycasts)

我一直在研究基於團隊的基本AI射擊系統,在該系統中,機器人可以向人類射擊,而人類可以在機器人射擊。 該系統正常工作,兩支隊伍都可以向對方射擊並殺死對方,但是一旦被殺死,它的射線廣播似乎無法檢測到敵人的對撞機。 這樣的結果是,殺死第一個機器人的機器人不再能被該機器人損壞,因此可以在重生時“扎營”。

編輯:當它們重生時,它們仍然照常進行射線投射,只是無法檢測到與對撞機的碰撞

我的射擊完全是通過射線廣播完成的,為了殺死/重生我的機器人,我將它們停用並將其移回到它們的重生位置。 通過NavMeshAgents和agent.destination完成對其他團隊的跟蹤。 以下是適用於它們的所有腳本。 對不起,如果很多。

Android射擊腳本

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

任何幫助將不勝感激。

找到解決方案歸功於Rutter:

禁用GameObject或Component會停止與其關聯的協程,並且重生bot停留在isShooting = true上,因此它們無法觸發。 我通過設置isShooting = false來修復它; 在重生功能中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM