Unity游戏的SetDestination和活动代理/ NavMesh问题

时间:2016-10-27 15:38:54

标签: c# unity3d unity5

我在游戏中的团结5中工作,我目前收到此错误:

“SetDestination”只能在已放置在NavMesh上的活动代理上调用。 UnityEngine.NavMeshAgent:SetDestination(的Vector3) EnemyMovement:Update()(在Assets / Scripts / Enemy / EnemyMovement.cs:25)

我的敌人运动脚本代码是:

using UnityEngine;
using System.Collections;

public class EnemyMovement : MonoBehaviour
{
Transform player;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
NavMeshAgent nav;


void Awake ()
{
    player = GameObject.FindGameObjectWithTag ("Player").transform;
    playerHealth = player.GetComponent <PlayerHealth> ();
    enemyHealth = GetComponent <EnemyHealth> ();
    nav = GetComponent <NavMeshAgent> ();
}


void Update ()
{
    if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
    {
        nav.SetDestination (player.position);
    }
    else
    {
        nav.enabled = false;
    }
 }
}

现在我知道这里几乎有一个相同的问题:Unity - "SetDestination" can only be called on an active agent that has been placed on a NavMesh. UnityEngine.NavMeshAgent:SetDestination(Vector3)然而,这个问题似乎没有得到明确的回应,我尝试做了一些回应,似乎没有任何效果。我会说我的游戏运行正常。但是,我添加了一种能力,如果玩家按下“B”,那么所有的敌人都会死在屏幕上。当我按下“B”时,敌人会死,但系统会“崩溃”,我会收到错误消息。

EnemyManager.cs:

        public void Kill(int InstanceID)
    {
        if (EnemyManager.Instance.EnemyList.ContainsKey(InstanceID))
            EnemyManager.Instance.EnemyList.Remove(InstanceID);
    }

    public void DeathToAll()
    {
        Dictionary<int, Object> TempEnemyList = new Dictionary<int, Object>(EnemyManager.Instance.EnemyList);
        foreach (KeyValuePair<int, Object> kvp in TempEnemyList)
        {
            // kvp.Key;   // = InstanceID
            // kvp.Value; // = GameObject

            GameObject go = (GameObject)kvp.Value;
            go.GetComponent<EnemyHealth>().Death();
        }

    }

Enemyhealth.cs

        public void Death ()
    {
        // The enemy is dead.
        isDead = true;

        // Turn the collider into a trigger so shots can pass through it.
        capsuleCollider.isTrigger = true;

        // Tell the animator that the enemy is dead.
        anim.SetTrigger ("Dead");

        // Change the audio clip of the audio source to the death clip and play it (this will stop the hurt clip playing).
        enemyAudio.clip = deathClip;
        enemyAudio.Play ();

        // Kill the Enemy (remove from EnemyList)
        // Get the game object associated with "this" EneymHealth script
        // Then get the InstanceID of that game object.
        // That is the game object that needs to be killed.
        EnemyManager.Instance.Kill(this.gameObject.GetInstanceID());


    }

1 个答案:

答案 0 :(得分:1)

这里的问题是,当您在致电nav.SetDestination()之前检查是否isDead时,当敌人被玩家杀死时,该值不会被更改能力。相反,似乎标志nav.SetDestination()设置在敌人身上,而不影响实际健康状况。

为了确保在敌人死亡时不会调用if(enemyHealth.currentHealth > 0 && !enemyHealth.isDead && playerHealth.currentHealth > 0) { nav.SetDestination (player.position); } (因为此时似乎敌人不再是有效的活动代理人),只需在if语句中添加一个附加条件:

isDead

希望这有帮助!如果您有任何疑问,请告诉我。 (您可能需要公开IsDead才能使其正常工作。)

另一种解决方案是创建一个属性enemyHealth.currentHealth > 0,它返回isDead,并通过将其生命值设置为0来杀死敌人。然后你不需要健康跟踪器和必须明确设置的单独{{1}}标志。这不仅仅是一个设计决策,因为它不会影响功能。