【问题标题】:GetRemainingDistance can only be called on an active agent that has been placed on a NavMeshGetRemainingDistance 只能在已放置在 NavMesh 上的活动代理上调用
【发布时间】:2016-07-23 01:27:33
【问题描述】:

我有两个错误:

第一个错误是:

MissingComponentException:没有“NavMeshAgent”附加到 "ThirdPersonController" 游戏对象,但脚本正在尝试访问 它。您可能需要将 NavMeshAgent 添加到游戏对象 “第三人称控制器”。或者您的脚本需要检查 组件在使用前已附加。

Patroll.Update ()(位于 Assets/My Scripts/Patroll.cs:41)

Patroll.Update 位于我创建的名为 Patroll.cs 的脚本文件中

using UnityEngine;
using System.Collections;

public class Patroll : MonoBehaviour {

    public Transform[] points;
    private int destPoint = 0;
    private NavMeshAgent agent;

    // Use this for initialization
    void Start () {

        agent = GetComponent<NavMeshAgent>();

        // Disabling auto-braking allows for continuous movement
        // between points (ie, the agent doesn't slow down as it
        // approaches a destination point).
        agent.autoBraking = false;

        GotoNextPoint();

    }

    void GotoNextPoint() {
        // Returns if no points have been set up
        if (points.Length == 0)
            return;

        // Set the agent to go to the currently selected destination.
        agent.destination = points[destPoint].position;

        // Choose the next point in the array as the destination,
        // cycling to the start if necessary.
        destPoint = (destPoint + 1) % points.Length;
    }


    void Update () {
        // Choose the next destination point when the agent gets
        // close to the current one.
        if (agent.remainingDistance < 0.5f)
            GotoNextPoint();
    }
}

第 41 行是:

if (agent.remainingDistance < 0.5f)

这个脚本 Patroll.cs 我拖到 Hierarchy to ThirdPersonController。

然后在此之后我遇到了另一个错误,甚至在我创建 Patroll.cs 脚本之前我也遇到了这个错误:

“GetRemainingDistance”只能在具有 已放置在 NavMesh 上。 UnityEngine.NavMeshAgent:get_remainingDistance() UnityStandardAssets.Characters.ThirdPerson.AICharacterControl:Update() (在资产/标准 Assets/Characters/ThirdPersonCharacter/Scripts/AICharacterControl.cs:31)

此错误在脚本 AICharacterControl.cs 中,它是统一脚本,也与层次结构中的 ThirdPersonController 有关。

第 31 行:

if (agent.remainingDistance > agent.stoppingDistance)

到目前为止,我试图解决它是统一的。我点击了 Component > Navigation > NavMesh Agent 上的菜单

现在它向 ThirdPersonController 添加了 Nav Nesh Agent,我可以在 ThirdPersonController 的 Inspector 中看到 Nav Nesh Agent 部分。

但错误仍然存​​在。

这是 AICharacterControl.cs 脚本

using System;
using UnityEngine;

namespace UnityStandardAssets.Characters.ThirdPerson
{
    [RequireComponent(typeof (NavMeshAgent))]
    [RequireComponent(typeof (ThirdPersonCharacter))]
    public class AICharacterControl : MonoBehaviour
    {
        public NavMeshAgent agent { get; private set; }             // the navmesh agent required for the path finding
        public ThirdPersonCharacter character { get; private set; } // the character we are controlling
        public Transform target;                                    // target to aim for


        private void Start()
        {
            // get the components on the object we need ( should not be null due to require component so no need to check )
            agent = GetComponentInChildren<NavMeshAgent>();
            character = GetComponent<ThirdPersonCharacter>();

            agent.updateRotation = false;
            agent.updatePosition = true;
        }


        private void Update()
        {
            if (target != null)
                agent.SetDestination(target.position);

            if (agent.remainingDistance > agent.stoppingDistance)
                character.Move(agent.desiredVelocity, false, false);
            else
                character.Move(Vector3.zero, false, false);
        }


        public void SetTarget(Transform target)
        {
            this.target = target;
        }
    }
}

我不知道如何修复错误。

【问题讨论】:

    标签: c# unity3d navmesh


    【解决方案1】:

    我在游戏中遇到了同样的问题。这发生在我在运行时加载角色预制件时,我的旧地图中的预制件有不同的位置。要解决这个问题,您可以将预制件放在导航网格上并保存您的预制件。

    【讨论】:

    • 您可以添加一些有关如何执行此操作的步骤吗?我不明白如何在导航网格上放置预制件?除非你指的是地形本身......
    【解决方案2】:

    检查您的警告,您可能会收到类似“无法创建代理,因为它离导航网格不够近”这样的消息。

    在unity中使用默认烘焙时出现此错误,请转到https://github.com/Unity-Technologies/NavMeshComponents并下载“NavMeshComponents”确保下载与您的unity版本匹配的版本,按照说明导入后,选择您的地板对象并添加“Nav Mesh Surface”脚本并烘焙它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      • 1970-01-01
      • 1970-01-01
      • 2018-10-04
      相关资源
      最近更新 更多