예제 #1
0
    public ControlledWaypoints NextWaypoint(ControlledWaypoints previousWaypoint)
    {
        if (connections.Count == 0)
        {
            Debug.LogError("Insufficient waypoint count.");
            return(null);
        }
        else if (connections.Count == 1 && connections.Contains(previousWaypoint))
        {
            return(previousWaypoint);
        }
        else
        {
            ControlledWaypoints nextWaypoint;
            int nextIndex = 0;

            do
            {
                nextIndex    = UnityEngine.Random.Range(0, connections.Count);
                nextWaypoint = connections[nextIndex];
            }while (nextWaypoint == previousWaypoint);
            {
                return(nextWaypoint);
            }
        }
    }
예제 #2
0
    private void SetDestination()
    {
        if (waypointsVisited > 0)
        {
            ControlledWaypoints nextWaypoint = currentWaypoint.NextWaypoint(previousWaypoint);
            previousWaypoint = currentWaypoint;
            currentWaypoint  = nextWaypoint;
        }

        Vector3 targetVector = currentWaypoint.transform.position;

        navMeshAgent.SetDestination(targetVector);
        travelling = true;
    }
예제 #3
0
    // Use this for initialization
    void Start()
    {
        GameObject[] allWaypoints = GameObject.FindGameObjectsWithTag("Waypoint");

        connections = new List <ControlledWaypoints>();

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

            if (nextWaypoint != null)
            {
                if (Vector3.Distance(this.transform.position, nextWaypoint.transform.position) <= connectedRadius && nextWaypoint != this)
                {
                    connections.Add(nextWaypoint);
                }
            }
        }
    }
예제 #4
0
    // Use this for initialization
    void Start()
    {
        navMeshAgent = GetComponent <NavMeshAgent>();

        if (navMeshAgent == null)
        {
            Debug.LogError("The nav mesh agent component is not attahced to " + gameObject.name);
        }
        else
        {
            if (currentWaypoint == null)
            {
                GameObject[] allWaypoints = GameObject.FindGameObjectsWithTag("Waypoint");

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

                        if (startingWaypoint != null)
                        {
                            currentWaypoint = startingWaypoint;
                        }
                    }
                }
                else
                {
                    Debug.LogError("Failed to find any waypoints for use in the scene.");
                }
            }
        }

        SetDestination();
    }