我尝试在Unity中为AI编写脚本,但我收到以下错误:错误CS1061:'NavMeshAgent'不包含'setDestination'的定义,而且找不到可接受第一个'NavMeshAgent'型别参数的可存取扩充方法'setDestination'(您是否遗漏using指示词或组件指涉?)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class enemyAI : MonoBehaviour
{
public NavMeshAgent agent;
public Transform player;
public LayerMask whatIsGround, whatIsPlayer;
// Patrolling
public Vector3 walkPoint;
bool walkPointSet;
public float walkPointRange;
// Attacking
public float timeBetweenAttacks;
bool alreadyAttacked;
// States
public float sightRange, attackRange;
public bool playerInSightRange, playerInAttackRange;
private void Awake()
{
player = GameObject.Find("player").transform;
agent = GetComponent<NavMeshAgent>();
}
private void Update()
{
// Check for in sight range and in attack range
playerInSightRange = Physics.CheckSphere(transform.position, sightRange,whatIsPlayer);
playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);
if (!playerInSightRange && !playerInAttackRange) Patroling();
if (playerInSightRange && !playerInAttackRange) ChasePlayer();
if (playerInSightRange && playerInAttackRange) AttackPlayer();
}
private void Patroling()
{
if (!walkPointSet) SearchWalkPoint();
if (walkPointSet)
{
agent.setDestination(walkPoint);
}
Vector3 distanceToWalkPoint = transform.position - walkPoint;
if (distanceToWalkPoint.magnitude < 1f)
{
walkPointSet = false;
}
}
private void SearchWalkPoint()
{
// Calculate random point in range
float randomZ = Random.Range(-walkPointRange, walkPointRange);
float randomX = Random.Range(-walkPointRange, walkPointRange);
walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);
if (Physics.Raycast(walkPoint, -transform.up, 2f, whatIsGround))
{
walkPointSet = true;
}
}
private void ChasePlayer()
{
agent.setDestination(player.position);
}
private void AttackPlayer()
{
agent.setDestination(transform.position);
transform.LookAt(player);
if (!alreadyAttacked)
{
//AttackCode here
Debug.Log("Enemy is attacking!!!");
alreadyAttacked = true;
Invoke(nameof(ResetAttack), timeBetweenAttacks);
}
}
private void ResetAttack()
{
alreadyAttacked = false;
}
}
I've gotten 3 errors for the 3 times I use .setDestination(), I don't know what I'm doing wrong...
1条答案
按热度按时间kmpatx3s1#
setDestination
中的“s”必须大写,因此它将是agent.SetDestination(transform.position);