public Transform GetWaypointTransform()
    {
        // if we have no waypoint transform already 'in the system' then we need to grab one
        if (currentWaypointTransform == null)
        {
            currentWaypointNum       = 0;
            currentWaypointTransform = waypointManager.GetWaypoint(currentWaypointNum);
        }

        return(currentWaypointTransform);
    }
Пример #2
0
    public void SetWayController(Waypoints_Controller aControl)
    {
        myWayControl = aControl;
        aControl     = null;

        // grab total waypoints
        totalWaypoints = myWayControl.GetTotal();

        // make sure that if you use SetReversePath to set shouldReversePathFollowing that you
        // call SetReversePath for the first time BEFORE SetWayController, otherwise it won't set the first waypoint correctly

        if (shouldReversePathFollowing)
        {
            currentWaypointNum = totalWaypoints - 1;
        }
        else
        {
            currentWaypointNum = 0;
        }

        Init();

        // get the first waypoint from the waypoint controller
        currentWaypointTransform = myWayControl.GetWaypoint(currentWaypointNum);

        if (startAtFirstWaypoint)
        {
            // position at the currentWaypointTransform position
            myTransform.position = currentWaypointTransform.position;
        }
    }
Пример #3
0
    public void SetWayController(Waypoints_Controller aControl)
    {
        if (aControl == null)
        {
            return;
        }

        myWayControl = aControl;

        // grab total waypoints
        totalWaypoints = myWayControl.GetTotal();

        if (shouldReversePathFollowing)
        {
            currentWaypointNum = totalWaypoints - 1;
        }
        else
        {
            currentWaypointNum = 0;
        }

        if (myWayControl.Closed)
        {
            loopPath = true;
        }
        else
        {
            loopPath = false;
        }

        Init();

        // get the first waypoint from the waypoint controller
        currentWaypointTransform = myWayControl.GetWaypoint(currentWaypointNum);

        if (startAtFirstWaypoint)
        {
            // position at the currentWaypointTransform position
            myTransform.position = currentWaypointTransform.position;
        }
    }
Пример #4
0
    private void UpdateWaypoints()
    {
        // If we don't have a waypoint controller, we safely drop out
        if (myWayControl == null)
        {
            return;
        }

        if (reachedLastWaypoint && destroyAtEndOfWaypoints)
        {
            // destroy myself(!)
            Destroy(gameObject);
            return;
        }
        else if (reachedLastWaypoint)
        {
            currentWaypointNum  = 0;
            reachedLastWaypoint = false;
        }

        // because of the order that scripts run and are initialised, it is possible for this function
        // to be called before we have actually finished running the waypoints initialization, which
        // means we need to drop out to avoid doing anything silly or before it breaks the game.
        if (totalWaypoints == 0)
        {
            // grab total waypoints
            totalWaypoints = myWayControl.GetTotal();
        }

        if (currentWaypointTransform == null)
        {
            // grab our transform reference from the waypoint controller
            currentWaypointTransform = myWayControl.GetWaypoint(currentWaypointNum);
            return;
        }

        // now we check to see if we are close enough to the current waypoint
        // to advance on to the next one

        myPosition   = myTransform.position;
        myPosition.z = 0;

        // get waypoint position and 'flatten' it
        nodePosition   = currentWaypointTransform.position;
        nodePosition.z = 0;

        // check distance from this to the waypoint
        currentWayDist = Vector3.Distance(nodePosition, myPosition);

        if (currentWayDist < waypointDistance)
        {
            // we are close to the current node, so let's move on to the next one!

            if (shouldReversePathFollowing)
            {
                currentWaypointNum--;
                // now check to see if we have been all the way around
                if (currentWaypointNum < 0)
                {
                    // just incase it gets referenced before we are destroyed, let's keep it to a safe index number
                    currentWaypointNum = 0;
                    // completed the route!
                    reachedLastWaypoint = true;
                    // if we are set to loop, reset the currentWaypointNum to 0
                    if (loopPath)
                    {
                        currentWaypointNum = totalWaypoints - 1;

                        // grab our transform reference from the waypoint controller
                        currentWaypointTransform = myWayControl.GetWaypoint(currentWaypointNum);

                        // the route keeps going in a loop, so we don't want reachedLastWaypoint to ever become true
                        reachedLastWaypoint = false;
                    }
                    // drop out of this function before we grab another waypoint into currentWaypointTransform, as
                    // we don't need one and the index may be invalid
                    return;
                }
            }
            else
            {
                currentWaypointNum++;
                // now check to see if we have been all the way around
                if (currentWaypointNum >= totalWaypoints)
                {
                    // completed the route!
                    reachedLastWaypoint = true;
                    // if we are set to loop, reset the currentWaypointNum to 0
                    if (loopPath)
                    {
                        currentWaypointNum = 0;

                        // grab our transform reference from the waypoint controller
                        currentWaypointTransform = myWayControl.GetWaypoint(currentWaypointNum);

                        // the route keeps going in a loop, so we don't want reachedLastWaypoint to ever become true
                        reachedLastWaypoint = false;
                    }
                    // drop out of this function before we grab another waypoint into currentWaypointTransform, as
                    // we don't need one and the index may be invalid
                    return;
                }
            }

            // grab our transform reference from the waypoint controller
            currentWaypointTransform = myWayControl.GetWaypoint(currentWaypointNum);
        }
    }