public Connected_WayPoints NextWayPoint(Connected_WayPoints previousWayPoint)
    {
        //no way points
        if (_connections.Count == 0)
        {
            return(null);
        }
        //
        else if (_connections.Count == 1 && _connections.Contains(previousWayPoint))
        {
            return(previousWayPoint);
        }
        else
        {
            Connected_WayPoints nextWayPoint;
            int nextIndex = 0;

            do
            {
                nextIndex    = UnityEngine.Random.Range(0, _connections.Count);
                nextWayPoint = _connections[nextIndex];
            }while (nextWayPoint == previousWayPoint);

            return(nextWayPoint);
        }
    }
Beispiel #2
0
    public void SetDestination_Waypoints()
    {
        Connected_WayPoints nextWayPoint = _currentWaypoint.NextWayPoint(_perviousWaypoint);

        _perviousWaypoint = _currentWaypoint;
        _currentWaypoint  = nextWayPoint;
        Vector3 targetVector = _currentWaypoint.transform.position;

        _navMeshAgent.SetDestination(targetVector);
        travelling = true;
    }
Beispiel #3
0
    void Start()
    {
        stateMachine = new StateMachine <Sleeper_AI>(this);
        stateMachine.ChangeState(Sleepingstate.Instance);
        thisGameObject = this.gameObject;
        player         = GameObject.FindGameObjectWithTag("Player");
        playerPosition = player.transform;
        _navMeshAgent  = this.GetComponent <NavMeshAgent>();
        hitReset       = 1;

        if (_navMeshAgent == null)
        {
            Debug.Log("A nav mesh agent is not attached to this game object.");
        }
        else
        {
            if (_currentWaypoint == null)
            {
                GameObject[] allWayPoints = GameObject.FindGameObjectsWithTag("WayPoint");

                if (allWayPoints.Length > 0)
                {
                    while (_currentWaypoint == null)
                    {
                        int random = UnityEngine.Random.Range(0, allWayPoints.Length);
                        Connected_WayPoints startingWaypoint = allWayPoints[random].GetComponent <Connected_WayPoints>();

                        if (startingWaypoint != null)
                        {
                            _currentWaypoint = startingWaypoint;
                        }
                    }
                }
                else
                {
                    Debug.Log("No way points in the scene");
                }
            }
        }
    }
    public void Start()
    {
        //Grab all Waypoints
        GameObject[] allWayPoints = GameObject.FindGameObjectsWithTag("WayPoint");

        //reference list
        _connections = new List <Connected_WayPoints>();

        //check if they are a connected way point
        for (int i = 0; i < allWayPoints.Length; i++)
        {
            Connected_WayPoints nextWayPoint = allWayPoints[i].GetComponent <Connected_WayPoints>();

            //found a way point
            if (nextWayPoint != null)
            {
                if (Vector3.Distance(this.transform.position, nextWayPoint.transform.position) <= _connectivityRadius && nextWayPoint != this)
                {
                    _connections.Add(nextWayPoint);
                }
            }
        }
    }