Target Closest Enemy - ForLily Method

This is my solution of the StTarget Enemy() Method:

    private void SetTargetEnemy()
    {
        
        EnemyMovement[] enemyList = FindObjectsOfType<EnemyMovement>();
        foreach (EnemyMovement enemy in enemyList)
        {
          float distanceToEnemy =  Vector3.Distance(transform.position, enemy.transform.position);
            if (targetEnemy)
            {
                if(distanceToEnemy < CheckDistance())
                {
                    targetEnemy = enemy.transform;
                }
            }
            else
            {
                targetEnemy = enemy.transform;
            }

        }
    }
   private float CheckDistance()
    {
    
        float distance = Vector3.Distance(targetEnemy.position, transform.position);
    
        return distance;
    }

Ben What do you think about it , In my game this works!

Privacy & Terms