Exemplo n.º 1
0
    /// <summary>
    /// Update flock waypoint
    /// </summary>
    void Update()
    {
        if (fleeing)
        {
            maxVelocity        = 6f;
            transform.position = (Vector2)playerObject.transform.position +
                                 (CenterOfFlock() - (Vector2)playerObject.transform.position).normalized * 200;
        }

        else
        {
            // check if we should flee
            int deadCount = 0;
            foreach (GameObject member in flockMembers)
            {
                if (member == null)
                {
                    deadCount++;
                }
            }
            if (deadCount > flockMembers.Length / 2)
            {
                fleeing = true;
            }

            // check distance to player - if within certain distance, set pos to player
            if (Vector2.Distance(CenterOfFlock(), playerObject.transform.position) < playerSightRange)
            {
                maxVelocity        = 30f;
                transform.position = playerObject.transform.position;
                if (waitingCoroutine != null)
                {
                    StopCoroutine(waitingCoroutine);
                    waitingCoroutine = null;
                }
            }

            // otherwise check if approximately at waypoint, if so set next waypoint
            else if (Vector2.Distance(CenterOfFlock(), transform.position) < 1f && waitingCoroutine == null)
            {
                currentWaypoint  = flockManager.GetNextWaypoint();
                waitingCoroutine = StartCoroutine(WaypointPause());
            }

            // if player just got out of sight, then start patrolling again
            else
            {
                maxVelocity        = 6f;
                transform.position = currentWaypoint.Position;
            }
        }
    }
Exemplo n.º 2
0
    // Use this for initialization
    void Start()
    {
        playerObject = GameObject.FindGameObjectWithTag("Player");

        flockManager = new FlockManager();
        foreach (GameObject go in Waypoints)
        {
            flockManager.AddWaypoint(go.GetComponent <FlockControllerWaypoint>());
        }

        flockMembers = new GameObject[numberOfMembers];
        for (int i = 0; i < numberOfMembers; i++)
        {
            Vector3 memberStartPosition = new Vector3(Random.Range(-range.x, range.x),
                                                      Random.Range(-range.y, range.y),
                                                      Random.Range(0, 0));
            flockMembers[i] = Instantiate(memberPrefab, this.transform.position + memberStartPosition,
                                          Quaternion.identity) as GameObject;
            flockMembers[i].GetComponent <FlockMember>().SetManager = this.gameObject;
        }

        currentWaypoint = flockManager.GetNextWaypoint();
    }