예제 #1
0
    //TODO Change time to a constant speed based on rough estimations of distance.
    //TODO Add an interrupt variable so that the AI can leave the path or change paths. Maybe check to see if destination changes.
    IEnumerator PathTravel(pathNode location, pathNode destination, float speed)
    {
        List <pathNode> path = Dijkstra.ShortestPath(NodePath, location.ID, destination.ID);

        if (path.Count > 1)
        {
            //Add initial location twice to initialize spline cap
            path.Insert(0, path[0]);
            //Add destination twice for the end cap on spline
            path.Add(path[path.Count - 1]);

            float timer = 0;
            for (int i = 0; i < path.Count - 3; i++)
            {
                timer = 0;
                pathNode nextNode = path[i + 2];
                float    dist     = Spline_Catmull_Rom.LengthofCurve(path[i].position, path[i + 1].position, path[i + 2].position, path[i + 3].position, 5);
                float    time     = dist / speed;
                while (Vector3.Distance(transform.position, nextNode.position) > Time.deltaTime)
                {
                    timer += Time.deltaTime;
                    if (timer > time)
                    {
                        break;
                    }
                    transform.position = Spline_Catmull_Rom.PointOnCurve(path[i].position, path[i + 1].position, path[i + 2].position, path[i + 3].position, timer / time);
                    transform.LookAt(Spline_Catmull_Rom.PointOnCurve(path[i].position, path[i + 1].position, path[i + 2].position, path[i + 3].position, (timer + Time.deltaTime) / time));
                    yield return(null);
                }
            }
            currentNode = destination.ID;
            traveling   = false;
        }
    }
예제 #2
0
    //Points in the direction of the path toward ID X
    public GameObject GetDirectionalArrow(int ID)
    {
        List <pathNode> path    = Dijkstra.ShortestPath(NodePath, currentNode, ID);
        GameObject      tempObj = Instantiate(arrow) as GameObject;

        //Add initial location twice to initialize spline cap
        path.Insert(0, path[0]);
        //Add destination twice for the end cap on spline
        path.Add(path[path.Count - 1]);
        tempObj.transform.position = Spline_Catmull_Rom.PointOnCurve(path[0].position, path[1].position, path[2].position, path[3].position, 0.3f);
        tempObj.transform.LookAt(Spline_Catmull_Rom.PointOnCurve(path[0].position, path[1].position, path[2].position, path[3].position, 0.5f));
        return(tempObj);
    }