【问题标题】:Unity - "SetDestination" can only be called on an active agent that has been placed on a NavMesh. UnityEngine.NavMeshAgent:SetDestination(Vector3)Unity - “SetDestination”只能在已放置在 NavMesh 上的活动代理上调用。 UnityEngine.NavMeshAgent:SetDestination(Vector3)
【发布时间】:2022-05-15 01:12:13
【问题描述】:

我现在正在使用 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

【问题讨论】:

  • 您是否在场景中制作了导航网格?代理在那个网格上吗?
  • 如何确定?

标签: c# unity3d


【解决方案1】:

您需要先将 Navmesh 添加到场景中,然后才能使用 NavMeshAgent 或与导航相关的任何其他内容。

Here's some videos by Unity about Navigation

【讨论】:

    【解决方案2】:

    1.确保您的thirdPersonController 在地面之上。

    2.进入Window->Navigation->Bake,点击bake。蓝色区域是第三人在地里可以到达的地方。

    【讨论】:

      【解决方案3】:
      1. 您的地板(平面或四边形)必须是静态的。
      2. 为你的玩家或你的敌人添加 NavmeshAgent 组件。

      您可以在检查面板上找到静态复选框

      【讨论】:

      • 3.在检查器面板上找到导航选项卡并单击烘焙按钮,您的地板颜色变为蓝色
      【解决方案4】:

      解决方案不是使用: navMeshAgent.enabled = false 你必须使用: navMeshAgent.isStopped = true

      【讨论】:

      • “任何能让提问者朝着正确方向前进的答案都是有帮助的,但请尝试在答案中提及任何限制、假设或简化。简洁是可以接受的,但更全面的解释会更好." - 检查How do I write a good answer.
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-24
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-07
      • 2022-08-03
      相关资源
      最近更新 更多