Первая ошибка:
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();
}
}
Код: Выделить всё
if (agent.remainingDistance < 0.5f)
После этого у меня возникает еще одна ошибка, и эта ошибка у меня тоже была еще до того, как я создал скрипт 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)
Теперь он добавил к 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
Мобильная версия