Unity - “SetDestination”只能在已放置在NavMesh上的活动代理上调用。 UnityEngine.NavMeshAgent:SetDestination(的Vector3)

时间:2015-05-28 04:07:32

标签: c# unity3d

我现在正在使用Unity5。尝试setDestination时出现此错误。

  

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

我的代码。

using UnityEngine;
using System.Collections;

namespace CompleteProject
{
    public class EnemyMovement : MonoBehaviour
    {
        Transform player;               // Reference to the player's position.
        PlayerHealth playerHealth;      // Reference to the player's health.
        EnemyHealth enemyHealth;        // Reference to this enemy's health.
        NavMeshAgent nav;               // Reference to the nav mesh agent.


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


        void Update ()
        {
            // If the enemy and the player have health left...
            if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
            {
                // ... set the destination of the nav mesh agent to the player.
                nav.SetDestination (player.position);
            }
            // Otherwise...
            else
            {
                // ... disable the nav mesh agent.
                nav.enabled = false;
            }
        }
    }
}

请指教。谢谢。

参考:https://github.com/datomnurdin/SurvivalShooter

3 个答案:

答案 0 :(得分:3)

在使用NavMeshAgent或与导航相关的任何其他内容之前,您需要将Navmesh添加到场景中。

Here's some videos by Unity about Navigation

答案 1 :(得分:2)

1.确保你的第三个控制器位于地面以上。

2.转到窗口 - &gt;导航 - &gt;烘烤并点击烘烤。蓝色区域是第三方可以到达地面的地方。

答案 2 :(得分:0)

  1. 你的地板(飞机或四边形)必须是静止的。
  2. 为玩家或敌人添加NavmeshAgent组件。
  3. 您可以在检查器面板上找到静态复选框