В Unity3D я создал персонажа зомба ie с простым вражеским сценарием ai, чтобы зомб ie преследовал игрока, но когда я добавил компонент NavMeshAgent, он объявляет следующее сообщение об ошибке:
«SetDestination» может быть вызван только для активного агента, который был помещен в NavMe sh .UnityEngine.AI.NavMeshAgent: SetDestination (Vector3)
Я уже пытался испечь NavMe sh но он не работает, пожалуйста, помогите, я застрял. Вот мой код:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.UIElements;
public class EnemyAI : MonoBehaviour
{
public float lookRadius = 20f;
Transform target;
NavMeshAgent agent;
// Start is called before the first frame update
void Start()
{
agent = GetComponent<NavMeshAgent>();
target = PlayerManager.instance.player.transform;
}
// Update is called once per frame
void Update()
{
float distance = Vector3.Distance(target.position, transform.position);
if (distance <= lookRadius)
{
agent.SetDestination(target.position);
}
if (distance <= agent.stoppingDistance)
{
FaceTarget();
}
void FaceTarget()
{
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
}
}
public void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, lookRadius);
}
}