Ejemplo n.º 1
0
    public static NavigationIsland[] GetPath(Vector3 a, Vector3 b)
    {
        NavigationIsland start = NavMeshCell.GetIslandFromPosition(NavigationMesh.mainMesh, a);

        if (start == null)
        {
            return(new NavigationIsland[] { });
        }

        NavigationIsland end = NavMeshCell.GetIslandFromPosition(NavigationMesh.mainMesh, b);

        if (end == null)
        {
            return(new NavigationIsland[] { });
        }

        var output = AStar.FindRoute(
            start,
            end,
            (x, y) => Vector3.Distance(x.center, y.center),
            (x, y) => Vector3.Distance(x.center, y.center),
            x => x.GetConnectedIslands());

        return(output.ToArray());
    }
Ejemplo n.º 2
0
    private void Update()
    {
        var path = Navigation.GetPath(a.transform.position, b.transform.position);

        foreach (var element in path)
        {
            DebugDraw.Cross(element.center, Quaternion.identity, 1f);
        }

        foreach (var(a, b) in path.ForeachElementAndNext())
        {
            Debug.DrawLine(a.center, b.center);
        }

        var islandA = NavMeshCell.GetIslandFromPosition(NavigationMesh.mainMesh, a.transform.position);
        var islandB = NavMeshCell.GetIslandFromPosition(NavigationMesh.mainMesh, b.transform.position);

        if (islandA != null)
        {
            DebugDraw.Cross(islandA.center, Quaternion.identity, 1f, Color.red);
        }

        if (islandB != null)
        {
            DebugDraw.Cross(islandB.center, Quaternion.identity, 1f, Color.red);
        }
    }