我有两个错误:
第一个错误是:
MissingComponentException:没有' NavMeshAgent'附在 " ThirdPersonController"游戏对象,但脚本正试图访问 它。您可能需要向游戏对象添加NavMeshAgent " ThirdPersonController&#34 ;.或者你的脚本需要检查是否 组件在使用前已附上。
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脚本之前也遇到了这个错误:
&#34; GetRemainingDistance&#34;只能在具有的活动代理上调用 被放置在NavMesh上。 UnityEngine.NavMeshAgent:get_remainingDistance() UnityStandardAssets.Characters.ThirdPerson.AICharacterControl:更新() (在Assets / Standard 资产/字符/ ThirdPersonCharacter /脚本/ AICharacterControl.cs:31)
此错误在脚本AICharacterControl.cs它的统一脚本中,并且还与层次结构中的ThirdPersonController相关。
第31行:
if (agent.remainingDistance > agent.stoppingDistance)
到目前为止我试图解决的问题是团结一致。我点击了Component&gt;上的菜单导航&gt;导航代理
现在它将添加到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;
}
}
}
我无法弄清楚如何修复错误。
答案 0 :(得分:0)
我在游戏中遇到了同样的问题。当我在运行时加载角色预制件时,我的旧地图中的预制件具有不同的位置。要解决此问题,您可以将预制件放在导航网格上并保存预制件。
答案 1 :(得分:0)
检查警告,您可能会收到类似“由于无法与NavMesh距离太近而无法创建代理”之类的消息。
在使用默认的统一烘焙时出现此错误,请转到https://github.com/Unity-Technologies/NavMeshComponents并下载“ NavMeshComponents”,请确保下载与您的统一版本匹配的版本,然后按照说明将其导入,然后选择地板对象,然后添加“导航网格曲面”脚本并将其烘焙。