Exemplo n.º 1
0
    void Start()
    {
        movementManager = GetComponent <MovementManager>();
        CurrentState    = AI_STATES.IDLE;

        if (testMode)
        {
            Vector3[] path = new Vector3[testWaypoints.Length];
            for (int i = 0; i < testWaypoints.Length; i++)
            {
                path[i] = testWaypoints[i].position;
            }
            currentPath = new Path(path);

            if (pathFollowRoutine != null)
            {
                StopCoroutine(pathFollowRoutine);
            }

            pathFollowRoutine = FollowPath();
            StartCoroutine(pathFollowRoutine);

            CurrentState = AI_STATES.EN_ROUTE;
        }
    }
    public void ChangeState(AI_STATES newState)
    {
        //Note the time I entered the state
        stateStartTime = Time.time;
        currentState   = newState;

        //Reset the avoidance state
        currentAvoidState = AI_AVOID_STATES.None;
    }
Exemplo n.º 3
0
    IEnumerator FollowPath()
    {
        while (currentPath.waypointIdx < currentPath.waypoints.Length)
        {
            StopCoroutine("MoveToNextWaypoint");
            yield return(StartCoroutine("MoveToNextWaypoint"));

            print("current waypoint index: " + currentPath.waypointIdx + "| waypoints.Length: " + currentPath.waypoints.Length);
        }

        currentPath  = null;
        CurrentState = AI_STATES.IDLE;
    }
Exemplo n.º 4
0
    void OnPathFound(Vector3[] _path, bool success)
    {
        // test manual path
        if (testMode)
        {
            return;
        }

        if (success && _path.Length > 0)
        {
            // Because the pathfinder doesn't immediately return a path (and the delay can get relatively long if many requests are in the queue), the unit may not be in the same position
            // it was in when the request was made, and the unit will be somewhat displaced from the starting node of the path. To avoid a kind of 'backtracking' behavior that can arise from
            // this, once the unit recieves its path, we use the same logic as in the pathfinding script to check to see if the SECOND point in the path is walkable from the unit's current position.
            // If it is, we can skip the first node in the path. If it isn't, then the backtracking behavior is acceptable, and in fact, ideal.

            // GOTCHA: This approach doesn't work well when traveling up a slope. An alternative approach would be to check if the first point in the path is sufficiently close to
            // the current position of the nav point. If it is, skip it.

            //print(Walkable(NavPoint, _path[1], 0.5f));

            if (_path.Length > 1 && Walkable(movementManager.NavPoint, _path[1], 0.5f))
            {
                _path[0] = movementManager.NavPoint;
            }

            currentPath        = new Path(_path);
            currentPath.radius = pathRadius;

            if (pathFollowRoutine != null)
            {
                StopCoroutine(pathFollowRoutine);
            }

            pathFollowRoutine = FollowPath();
            StartCoroutine(pathFollowRoutine);

            CurrentState = AI_STATES.EN_ROUTE;
        }
    }
Exemplo n.º 5
0
 public void ChangeState(AI_STATES newState)
 {
     stateStartTime = Time.time;
     currentState   = newState;
 }