Example #1
0
    public EnemyConnectedWaypoint NextWaypoint(EnemyConnectedWaypoint previousWaypoint)
    {
        if (connections.Count == 0)
        {
            Debug.LogError("Need waypoints! NOW!");
            return(null);
        }
        else if (connections.Count == 1 && connections.Contains(previousWaypoint))
        {
            return(previousWaypoint);
        }
        else
        {
            EnemyConnectedWaypoint nextWaypoint;
            int nextIndex = 0;

            do
            {
                nextIndex    = Random.Range(0, connections.Count);
                nextWaypoint = connections[nextIndex];
            }while (nextWaypoint == previousWaypoint);

            return(nextWaypoint);
        }
    }
Example #2
0
    public void Start()
    {
        agent    = this.GetComponent <NavMeshAgent>();
        behavior = this.GetComponent <EnemyBehavior>();

        if (currentWaypoint == null)
        {
            GameObject[] allWaypoints = GameObject.FindGameObjectsWithTag("Waypoint");

            if (allWaypoints.Length > 0)
            {
                while (currentWaypoint == null)
                {
                    int random = Random.Range(0, allWaypoints.Length);
                    EnemyConnectedWaypoint startingWaypoint = allWaypoints[random].GetComponent <EnemyConnectedWaypoint>();

                    // We found a waypoint
                    if (startingWaypoint != null)
                    {
                        currentWaypoint = startingWaypoint;
                    }
                }
            }
            else
            {
                Debug.LogError("Failed to find waypoints in this scene. NEED WAYPOINTS. NOW!");
            }
        }

        SetDestination();
    }
Example #3
0
    private void SetDestination()
    {
        if (waypointsVisited > 0)
        {
            EnemyConnectedWaypoint nextWaypoint = currentWaypoint.NextWaypoint(previousWaypoint);
            previousWaypoint = currentWaypoint;
            currentWaypoint  = nextWaypoint;
        }

        Vector3 targetVector = currentWaypoint.transform.position;

        agent.SetDestination(targetVector);
        travelling = true;
    }
Example #4
0
    public void Start()
    {
        // Grab all waypoints in scene
        GameObject[] allWaypoints = GameObject.FindGameObjectsWithTag("Waypoint");

        // Create a list of waypoints I can refer to later
        connections = new List <EnemyConnectedWaypoint>();

        for (int i = 0; i < allWaypoints.Length; i++)
        {
            EnemyConnectedWaypoint nextWaypoint = allWaypoints[i].GetComponent <EnemyConnectedWaypoint>();

            // We find a waypoint
            if (nextWaypoint != null)
            {
                if (Vector3.Distance(this.transform.position, nextWaypoint.transform.position) <= connectivityRadius && nextWaypoint != this)
                {
                    connections.Add(nextWaypoint);
                }
            }
        }
    }