示例#1
0
    void UpdatePath()
    {
        if (seeker.IsDone() && !isDead)
        {
            if (gameManagerScript.LastRobotDead && lastRobotNumber != gameManagerScript.RobotNumber)
            {
                lastRobotNumber         = gameManagerScript.RobotNumber;
                ShouldUpdatePlayerArray = true;
            }
            if (ShouldUpdatePlayerArray)
            {
                GameObject[] tag1 = GameObject.FindGameObjectsWithTag("Player");
                GameObject[] tag2 = GameObject.FindGameObjectsWithTag("Robot");
                player = tag1.Concat(tag2).ToArray();
                ShouldUpdatePlayerArray = false;
            }
            //checking nearest target
            float lastDistance = 10000f;
            foreach (GameObject currentPlayer in player)
            {
                float currentDistance;
                try
                {
                    currentDistance = Vector2.Distance(currentPlayer.transform.position, gameObject.transform.position);
                } catch (MissingReferenceException e)
                {
                    ShouldUpdatePlayerArray = true;
                    return;
                }

                if (lastDistance > currentDistance)
                {
                    lastDistance        = currentDistance;
                    target              = currentPlayer.transform;
                    nearestPlayer       = currentPlayer;
                    nearestPlayerScript = nearestPlayer.GetComponent <PlayerScript>();
                }
            }
            float distanceWithNearestPlayer = Vector2.Distance(nearestPlayer.transform.position, gameObject.transform.position);
            seeker.StartPath(rb.position, target.position, onPathComplete);
            float length = 10f;
            if (path != null)
            {
                length = path.GetTotalLength();
            }

            for (int i = 1; i < player.Length; i++)
            {
                Transform temp;
                try
                {
                    temp = player[i].GetComponent <Transform>();
                }
                catch (MissingReferenceException e)
                {
                    ShouldUpdatePlayerArray = true;
                    return;
                }
                //seeker.StartPath(rb.position, target.position, onPathComplete);
                float tempLength = path.GetTotalLength();
                if (tempLength < length)
                {
                    target = temp;
                    length = tempLength;
                }
            }
            if (length < detRange)
            {
                pDetected = true;
                if (isBoss)
                {
                    detRange = 10000f;
                    if (!isBossMusicPlaying)
                    {
                        isBossMusicPlaying = true;
                        uiScript.PlayBossMusic(sndBoss);
                        uiScript.ActiveFigthingBossUI(gameObject.GetComponentInChildren <EnemyHealthBarScript>(), bossName);
                    }
                }
                if (!OnAttack && distanceWithNearestPlayer < attackRange && nearestPlayerScript.healthpoints > 0f)
                {
                    OnAttack = true;
                    audioSource.PlayOneShot(sndAttack);
                    animator.SetTrigger("Attack");
                    if (IsRanged)
                    {
                        //IA Fire
                        Vector2      rayDir = target.position;
                        RaycastHit2D hit    = Physics2D.Raycast(transform.position, rayDir, attackRange, layer);
                        StartCoroutine(Arrow());
                    }
                    else
                    {
                        colliderRadius          = circleCollider2D.radius;
                        circleCollider2D.radius = AttackColliderRadius;
                    }
                    StartCoroutine(AttackBool());
                }
            }
        }
    }