コード例 #1
0
    public GameObject FindClosestEnemy()
    {
        //get list if all enemies.
        monsters = GameObject.FindGameObjectsWithTag("Monsters");

        monstersInRange = new List <GameObject> ();
        //Get search all enemies in range.
        foreach (GameObject monster in monsters)
        {
            float curDistance = (monster.transform.position - transform.position).sqrMagnitude;
            if (curDistance <= enemyDetectRange)
            {
                monstersInRange.Add(monster);
            }
        }

        //No enemies in range
        if (monstersInRange.Count == 0)
        {
            return(null);
        }

        //Find the closest enemy in enemiesInRange
        GameObject closest = null;

        //If one enemy, set only enemy
        if (monstersInRange.Count == 1)
        {
            closest = monstersInRange[0];
        }
        else if (monstersInRange.Count > 1)
        {
            //If the is multiple in range sort and find the closest
            for (int i = 1; i < monstersInRange.Count; i++)
            {
                //Set first enemy
                if (closest == null)
                {
                    closest = monstersInRange[0];
                }
                float cloDistance = (closest.transform.position - transform.position).sqrMagnitude;
                float curDistance = (monstersInRange[i].transform.position - transform.position).sqrMagnitude;
                //compare current enemy with another and set if closer.
                if (curDistance < cloDistance)
                {
                    closest = monstersInRange[i];

                    //Flip to face enemy
                    if (closest.transform.position.x > transform.position.x)
                    {
                        rcCharacter.flipPlayerRight();
                    }
                    else if (closest.transform.position.x < transform.position.x)
                    {
                        rcCharacter.flipPlayerLeft();
                    }
                }
            }
        }
        return(closest);
    }