NPC 在 Navmesh 上实例化
NPC instantiate on Navmesh
我正在尝试以网格方式在 NavMesh 上实例化 NPC 预制件。预制件有组件 NavMeshAgent 并且 NavMesh 已经被烘焙。我收到错误:
"SetDestination" can only be called on an active agent that has been placed on a NavMesh.
UnityEngine.AI.NavMeshAgent:SetDestination(Vector3)
和
"GetRemainingDistance" can only be called on an active agent that has been placed on a NavMesh.
UnityEngine.AI.NavMeshAgent:get_remainingDistance()
这在放置在 NavMesh 上的空游戏对象上使用以下脚本:
// Instantiates a prefab in a grid
public GameObject prefab;
public float gridX = 5f;
public float gridY = 5f;
public float spacing = 2f;
void Start()
{
for (int y = 0; y < gridY; y++)
{
for (int x = 0; x < gridX; x++)
{
Vector3 pos = new Vector3(x, 0, y) * spacing;
Instantiate(prefab, pos, Quaternion.identity);
}
}
}
首先我要说 NavMesh 有时非常棘手。涉及到如此多的小怪癖等,以至于我最终离开了 NavMesh,并使用了 A*(A 星)光线投射样式库。对于数十个同时移动的实体来说效率不高,但对于动态地图和 objects/model 攀爬非常通用。
我还要说的是,能够使用简单的 API 命令不足以使用 Nav Mesh - 您需要了解协同工作的众多组件,而 Unity 文档没有那么大的帮助应该如此。如果您使用的是动态实体并且需要重新雕刻等,请准备好拉出一些头发。
无论如何,我要警告你的第一件事是,如果你的实体周围有碰撞器,它们可能会干扰它们自己的导航(因为它们自己的碰撞器可以切入导航网格,使实体在一个小的非网格补丁)。
其次,我建议您将您的实体 Warp() 到导航网格上。这会获取您实体的位置(可能不是真正在导航网格上)并将其扭曲到关闭可用的 NavMesh 节点/link,此时它应该能够导航
祝你好运!
关于:
"SetDestination" 只能在已放置在 NavMesh 上的活动代理上调用。
UnityEngine.AI.NavMeshAgent:SetDestination(Vector3)
我认为问题在于您没有将相应的 NavMeshAgent
逻辑添加到您正在实例化的预制件中。执行以下操作:
- 将 Nav Mesh Agent 添加到您正在实例化的预制件中
- 为了测试,设置一个目标点(这可以是一个空的游戏对象),并将其命名为 "Destination"
- 将以下脚本添加到您要实例化的GameObject
像这样:
public class Movement : MonoBehaviour {
//Point towards the instantiated Object will move
Transform goal;
//Reference to the NavMeshAgent
UnityEngine.AI.NavMeshAgent agent;
// Use this for initialization
void Start () {
//You get a reference to the destination point inside your scene
goal = GameObject.Find("Destination").GetComponent<Transform>();
//Here you get a reference to the NavMeshAgent
agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
//You indicate to the agent to what position it has to move
agent.destination = goal.position;
}
}
如果您的实例化预制件需要追踪某些东西,您可以从 Update() 更新 goal.position。喜欢:
void Update(){
agent.destination = goal.position;
}
好的,问题已解决。根据我的 OP,我确实有一个烘焙的 Nav Mesh 和预制件有 Nav Mesh Agent 组件。问题是 Nav Mesh 的分辨率和 Nav Mesh Agent 上的 Base Offset 设置为 -0.2。
使用高度网格设置重新烘焙导航网格使可行走区域更加准确。
连同将 Nav Mesh Agent 上的 Base Offset 更改为 0。
我正在尝试以网格方式在 NavMesh 上实例化 NPC 预制件。预制件有组件 NavMeshAgent 并且 NavMesh 已经被烘焙。我收到错误:
"SetDestination" can only be called on an active agent that has been placed on a NavMesh.
UnityEngine.AI.NavMeshAgent:SetDestination(Vector3)
和
"GetRemainingDistance" can only be called on an active agent that has been placed on a NavMesh.
UnityEngine.AI.NavMeshAgent:get_remainingDistance()
这在放置在 NavMesh 上的空游戏对象上使用以下脚本:
// Instantiates a prefab in a grid
public GameObject prefab;
public float gridX = 5f;
public float gridY = 5f;
public float spacing = 2f;
void Start()
{
for (int y = 0; y < gridY; y++)
{
for (int x = 0; x < gridX; x++)
{
Vector3 pos = new Vector3(x, 0, y) * spacing;
Instantiate(prefab, pos, Quaternion.identity);
}
}
}
首先我要说 NavMesh 有时非常棘手。涉及到如此多的小怪癖等,以至于我最终离开了 NavMesh,并使用了 A*(A 星)光线投射样式库。对于数十个同时移动的实体来说效率不高,但对于动态地图和 objects/model 攀爬非常通用。
我还要说的是,能够使用简单的 API 命令不足以使用 Nav Mesh - 您需要了解协同工作的众多组件,而 Unity 文档没有那么大的帮助应该如此。如果您使用的是动态实体并且需要重新雕刻等,请准备好拉出一些头发。
无论如何,我要警告你的第一件事是,如果你的实体周围有碰撞器,它们可能会干扰它们自己的导航(因为它们自己的碰撞器可以切入导航网格,使实体在一个小的非网格补丁)。
其次,我建议您将您的实体 Warp() 到导航网格上。这会获取您实体的位置(可能不是真正在导航网格上)并将其扭曲到关闭可用的 NavMesh 节点/link,此时它应该能够导航
祝你好运!
关于:
"SetDestination" 只能在已放置在 NavMesh 上的活动代理上调用。 UnityEngine.AI.NavMeshAgent:SetDestination(Vector3)
我认为问题在于您没有将相应的 NavMeshAgent
逻辑添加到您正在实例化的预制件中。执行以下操作:
- 将 Nav Mesh Agent 添加到您正在实例化的预制件中
- 为了测试,设置一个目标点(这可以是一个空的游戏对象),并将其命名为 "Destination"
- 将以下脚本添加到您要实例化的GameObject
像这样:
public class Movement : MonoBehaviour {
//Point towards the instantiated Object will move
Transform goal;
//Reference to the NavMeshAgent
UnityEngine.AI.NavMeshAgent agent;
// Use this for initialization
void Start () {
//You get a reference to the destination point inside your scene
goal = GameObject.Find("Destination").GetComponent<Transform>();
//Here you get a reference to the NavMeshAgent
agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
//You indicate to the agent to what position it has to move
agent.destination = goal.position;
}
}
如果您的实例化预制件需要追踪某些东西,您可以从 Update() 更新 goal.position。喜欢:
void Update(){
agent.destination = goal.position;
}
好的,问题已解决。根据我的 OP,我确实有一个烘焙的 Nav Mesh 和预制件有 Nav Mesh Agent 组件。问题是 Nav Mesh 的分辨率和 Nav Mesh Agent 上的 Base Offset 设置为 -0.2。
使用高度网格设置重新烘焙导航网格使可行走区域更加准确。
连同将 Nav Mesh Agent 上的 Base Offset 更改为 0。