团结 - 敌人AI走过墙壁

时间:2016-10-12 11:31:54

标签: unity3d artificial-intelligence

我已经按照YouTube上的Unity视频为您的人工智能创建了状态机,但是,当敌人在巡逻时,他们会穿过墙壁。我试图将RigidBody组件添加到敌人身上,但这完全会阻止他们前往每个航路点的移动。

以下是使AI移动到每个航路点的代码

private void Look()
{
    RaycastHit hit;
    if (Physics.Raycast(enemy.eyes.transform.position, enemy.eyes.transform.forward, out hit, enemy.sightRange) && hit.collider.CompareTag("Player"))
    {
        enemy.chaseTarget = hit.transform;
        InChaseState();
    }

}

void Patrol()
{
    enemy.meshRendererFlag.material.color = Color.green; //Test to see what state the enemy is in
    enemy.navMeshAgent.destination = enemy.waypoints[nextWaypoint].position; //Select the current waypoint
    enemy.navMeshAgent.Resume(); //Start walking again

    if (enemy.navMeshAgent.remainingDistance <= enemy.navMeshAgent.stoppingDistance && !enemy.navMeshAgent.pathPending) //Checking to see if we've reached our destination
    {
        nextWaypoint = (nextWaypoint + 1) % enemy.waypoints.Length; //Go to next waypoint and should loop as expected
    }
}

任何人都知道如何编辑此代码以使敌人在地图中围绕墙壁走动以到达每个航路点?感谢。

1 个答案:

答案 0 :(得分:3)

听起来你的墙壁没有被烤到你的导航网格中。为此,如果您还没有,请通过单击Window-&gt; Navigation将导航窗口添加到编辑器。在场景中选择墙,在“对象”选项卡上的“导航”窗格中,选中“导航静态”框。然后按导航窗格右下角的烘焙按钮。如果你的墙有一个对撞机,你应该看到这样的东西:

shapely documentation

蓝色区域表示允许导航代理行走的地方。您会注意到墙壁周围的区域不是蓝色,因此您的代理商将无法穿过那里。

相关问题