GetRemainingDistance можно вызвать только для активного агента, размещенного в NavMesh.C#

Место общения программистов C#
Ответить
Anonymous
 GetRemainingDistance можно вызвать только для активного агента, размещенного в NavMesh.

Сообщение Anonymous »

У меня есть две ошибки:

Первая ошибка:



MissingComponentException: к игровому объекту
«ThirdPersonController» не прикреплен «NavMeshAgent», но скрипт пытается получить
доступ к нему. Вероятно, вам нужно добавить 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();

// 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)
То, что я пытался сделать до сих пор, чтобы исправить это, - это единство. Я щелкнул меню «Компонент» > «Навигация» > «Агент NavMesh».

Теперь он добавил к ThirdPersonController агент Nav Nesh, и я вижу в инспекторе ThirdPersonController часть агента 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();
character = GetComponent();

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;
}
}
}
Не могу понять, как исправить ошибки.

Подробнее здесь: https://stackoverflow.com/questions/385 ... een-placed
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»