对于敌人,我每隔x秒在导航网上设置一个随机位置,这样敌人就可以随意地四处走动。这里我的剧本:
随机点(来自统一文档)
bool RandomPoint (Vector3 center, float range, out Vector3 result)
{
for (int i = 0; i < 30; i++) {
Vector3 randomPoint = center + Random.insideUnitSphere * range;
NavMeshHit hit;
if (NavMesh.SamplePosition (randomPoint, out hit, 1.0f, NavMesh.AllAreas)) {
result = hit.position;
return true;
}
}
result = Vector3.zero;
return false;
}我的随机运动剧本:
IEnumerator Walking ()
{
if (!isFollowingPlayer && isServer) {
Debug.LogError ("-- Start Walking --");
agent.speed = 2;
Vector3 randomDirection;
bool blocked = RandomPoint (agent.transform.position, walkRadius, out randomDirection);
NavMeshHit hit;
blocked = NavMesh.Raycast (enemy.transform.position, randomDirection, out hit, NavMesh.AllAreas);
if (!blocked) {
geileMethodeZumZeichnen (agent);
target = hit.position;
agent.SetDestination (hit.position);
geileMethodeZumZeichnen (agent);
Debug.LogWarning ("Go to " + hit.position);
}
if (blocked) {
Debug.LogWarning ("-- BLOCKED --");
yield return new WaitForEndOfFrame ();
} else {
Debug.LogWarning ("-- WAIT --");
yield return new WaitForSeconds (5f);
}
StartCoroutine ("Walking");
}
}随机行走一般起作用。然而,当导航网格剂靠近导航网格的边界时,它似乎“爆发”了,然后被固定在自己的位置上。他只是在导航网外“焦虑不安”。(图像上的红线:路径)

看起来他们试图到达他们的位置,但不能再次进入导航网。我不希望他们首先离开:)
发布于 2016-07-04 13:47:03
好的,这最后很简单:我没有正确地设置物理。敌人真的“感觉”到边缘,并被困在那里,因为物理。现在工作得很有魅力。
https://stackoverflow.com/questions/38162772
复制相似问题