我现在正在使用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;
}
}
}
}
参考:https://github.com/datomnurdin/SurvivalShooter
最佳答案
您需要先向场景中添加一个导航网格,然后才能使用NavMeshAgent或与导航相关的任何其他功能。
Here's some videos by Unity about Navigation