GetRemainingDistance只能在已放置在NavMesh上的活动代理上调用

问题描述 投票:1回答:2

我有两个错误:

第一个错误是:

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

Payroll.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)

这个脚本Patrol.cs我拖到Hierarchy to Third Person Controller。

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

只能在已放置在NavMesh上的活动代理上调用“GetRemainingDistance”。 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 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 unityscript unity5 navmesh
2个回答
0
投票

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


0
投票

检查您的警告,您可能会收到“无法创建代理,因为它与NavMesh不够接近”的消息。

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

© www.soinside.com 2019 - 2024. All rights reserved.