【发布时间】:2022-05-15 01:12:13
【问题描述】:
我现在正在使用 Unity5。尝试 setDestination 时出现此错误。
“SetDestination”只能在已放置在 NavMesh 上的活动代理上调用。 UnityEngine.NavMeshAgent:SetDestination(Vector3) CompleteProject.EnemyMovement:Update()(在 Assets/_CompletedAssets/Scripts/Enemy/EnemyMovement.cs:30)
我的代码。
using UnityEngine;
using System.Collections;
namespace CompleteProject
{
public class EnemyMovement : MonoBehaviour
{
Transform player; // Reference to the player's position.
PlayerHealth playerHealth; // Reference to the player's health.
EnemyHealth enemyHealth; // Reference to this enemy's health.
NavMeshAgent nav; // Reference to the nav mesh agent.
void Awake ()
å{
// Set up the references.
player = GameObject.FindGameObjectWithTag ("Player").transform;
playerHealth = player.GetComponent <PlayerHealth> ();
enemyHealth = GetComponent <EnemyHealth> ();
nav = GetComponent <NavMeshAgent> ();
}
void Update ()
{
// If the enemy and the player have health left...
if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
{
// ... set the destination of the nav mesh agent to the player.
nav.SetDestination (player.position);
}
// Otherwise...
else
{
// ... disable the nav mesh agent.
nav.enabled = false;
}
}
}
}
【问题讨论】:
-
您是否在场景中制作了导航网格?代理在那个网格上吗?
-
如何确定?