我正在尝试以网格方式在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()
这使用放置在GameObject上的空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);
}
}
}
发布于 2018-12-06 01:41:03
好吧,把问题解决了。据我的操作,我确实有一个烤Nav网格和预制板有Nav网格代理组件。问题是Nav网格和基础偏移量在Nav网格代理上的分辨率,该偏移量设置为-0.2。
用高度网格设置反弹Nav网格,使可行走区域更加精确。
以及将Nav网格代理上的基本偏移量更改为0。
发布于 2018-11-30 04:46:48
我首先要说的是,NavMesh有时非常棘手。有这么多的小怪癖等涉及到,我最终离开了NavMesh,并正在使用一个A* (A星)射线投射风格的库。对于数十个同时移动的实体来说,没有任何地方是有效的,但是对于动态映射和对象/模型爬升来说却是非常灵活的。
另外,我要说,能够使用简单的API命令并不足以使用Nav -您需要了解协同工作的众多组件,而Unity并没有它应有的帮助。如果你使用的是动态实体,并且需要回复等,那么要做好准备把头发拔出来。
无论如何,我要警告你的第一件事是,如果你的实体周围有对撞机,它们可能会干扰他们自己的导航(因为他们自己的对撞机可以插入导航网,把实体留在一小块非网格上)。
其次,我建议你把你的实体放到导航网上。这将获取实体的位置(可能不是在nav网格上),并将其扭曲到关闭可用的NavMesh节点/链接,此时它应该能够导航。
祝好运!
发布于 2018-11-30 06:28:58
关于:
"SetDestination“只能在放置在NavMesh上的活动代理上调用。UnityEngine.AI.NavMeshAgent:SetDestination(Vector3)
我认为问题在于,您没有将相应的NavMeshAgent
逻辑添加到正在实例化的预制板中。做以下工作:
就像这样:
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;
}
https://stackoverflow.com/questions/53550920
复制相似问题