【发布时间】: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;
}
}
}
我不知道如何修复错误。
【问题讨论】: