1

我有两个错误:

第一个错误是:

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

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 到 ThirdPersonController。

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

“GetRemainingDistance”只能在已放置在 NavMesh 上的活动代理上调用。UnityEngine.NavMeshAgent:get_remainingDistance() UnityStandardAssets.Characters.ThirdPerson.AICharacterControl:Update()(在 Assets/Standard Assets/Characters/ThirdPersonCharacter/Scripts/AICharacterControl.cs:31)

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

第 31 行:

if (agent.remainingDistance > agent.stoppingDistance)

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

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

但是错误仍然存​​在。

这是 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;
        }
    }
}

我不知道如何解决这些错误。

4

2 回答 2

0

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

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

于 2019-04-08T18:34:47.903 回答
0

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

于 2017-05-28T15:26:11.027 回答