[在Unity3D中,我用简单的敌人ai脚本创建了一个僵尸角色,以便僵尸追赶玩家,但是当我添加NavMeshAgent组件时,它声明了以下错误消息:
“ SetDestination”只能在已放置在NavMesh.UnityEngine.AI.NavMeshAgent:SetDestination(Vector3)]上的活动代理上调用
我已经尝试过烘焙NavMesh,但是它没有用,请帮助我。这是我的代码:
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);
}
}
[在Unity3D中,我用简单的敌人ai脚本创建了一个僵尸角色,因此僵尸将追赶玩家,但是当我添加NavMeshAgent组件时,它声明了以下错误消息:“ ...