Ejemplo n.º 1
0
    /* Go through all the enemies within the field of view and
     * check whether or not their are obstructed by an object.
     * If they are not, they are added to the visibleEnemies list */
    void FindVisibleEnemies()
    {
        // VisibleEnemies.visibleEnemies.Clear();
        // a list with all the enemy objects that are within the view radius
        Collider[] enemiesInViewRadius = Physics.OverlapSphere(transform.position, viewRadius, enemyMask);

        for (int i = 0; i < enemiesInViewRadius.Length; i++)
        {
            Transform enemy      = enemiesInViewRadius[i].transform;
            Vector3   dirToEnemy = (enemy.position - transform.position).normalized;

            if (Vector3.Angle(transform.forward, dirToEnemy) < viewAngle / 2)
            {
                float dstToEnemy = Vector3.Distance(transform.position, enemy.position);

                if (!Physics.Raycast(transform.position, dirToEnemy, dstToEnemy, objectMask))
                {
                    // VisibleEnemies.visibleEnemies.Add(enemy);
                    VisibleEnemies.AddVisibleEnemy(enemy);
                }
            }
        }
        // if (VisibleEnemies.OnEnemiesVisibilityChange != null) VisibleEnemies.OnEnemiesVisibilityChange(VisibleEnemies.visibleEnemies);
        // VisibleEnemies.EnemiesVisibilityChange();
    }
Ejemplo n.º 2
0
 public void FindTarget()
 {
     if (VisibleEnemies.Contains(Target))
     {
         // do nothing
     }
     else if (VisibleEnemies.Count > 0)
     {
         Vector3 thisPos = gameObject.transform.position;
         Target = VisibleEnemies[0];
         float shortestDistance = Vector3.Distance(thisPos, VisibleEnemies[0].transform.position);
         for (int i = 1; i < VisibleEnemies.Count; i++)
         {
             Vector3 enemyPos = VisibleEnemies[i].transform.position;
             if (Vector3.Distance(thisPos, enemyPos) < shortestDistance)
             {
                 Target = VisibleEnemies[i];
             }
         }
     }
     else
     {
         Target = null;
     }
 }