Any tips on my code (A.I)?

Hello, iam just new to programming (started 8 months ago) and i hope that i can improve my programming.

So i leave my code here, and hope that some people can learn me new tricks so that i can become a better programmer.

Cheers,

Briyan

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class AI_1st_Edition_Black : MonoBehaviour {
    //all gameobjects
    private List<GameObject> enemies = new List<GameObject> ();
    //float's
    //the health float
    public float health;
    //the distance float
    private float distance;
   
    //bool's
    //the is swinging bool
    public bool is_Swinging;
    //the can swing bool
    private bool can_Swing;
    //if the soldier is dead
    private bool dead;
    //alive
    private bool alive;
    //bene blocked
    public bool been_Blocked;
   
    //the navmeshagent
    private NavMeshAgent agent;
    //animator
    private Animator anim;
   
    //the animation walk
    public string walk;
    //the idle animation
    public string idle;
    //the attack animation
    public string attack;
    //dead
    public string dead_Anim;

    public Transform blood;


   
    //this is the start function
    void Start(){
        //the soldier can swing
        can_Swing = true;
        //the soldier isn't swinging
        is_Swinging = false;
        //the soldier isn't dead
        dead = false;
        //the health variable is 100
        health = 100.0f;
        //the distance is 0
        distance = 0.0f;
        //the enemies will be added with enemies ofcourse...
        enemies.AddRange (GameObject.FindGameObjectsWithTag ("White_Soldier"));
        //black
        enemies.Add (GameObject.FindGameObjectWithTag ("Player"));
        //the navmeshagent
        agent = GetComponent<NavMeshAgent> ();
        //the animator
        anim = GetComponent<Animator> ();
        //alive is true
        alive = true;
        //the blood is false
        blood.gameObject.SetActive(false);
    }
   
    void Update(){
        //when the soldier is alive
        if (alive == true) {
            //then the brain will be used
            Brain ();
        } else if(health <= 0) {
            //the soldier is dead, the dead function will be called now
            Dead();
        }
    }
   
    void Dead(){
        gameObject.tag = "Dead";
        agent.enabled = false;
        blood.gameObject.SetActive (true);
        anim.Play (dead_Anim);
    }
   
    //this is the brain
    void Brain(){
        //here will the distance be calculated
        distance = Vector3.Distance (transform.position, GetClosestEnemy ().transform.position);
        //when the distance is between 50 meters
        if (distance <= 50 && distance > 2.0f)
        {
            //the destination will be set
            agent.SetDestination(GetClosestEnemy().transform.position);
            //in case the agent has been stopped
            agent.Resume();
        }
        else if (distance <= 2.0f)
        {
            //the agent will be stopped
            agent.Stop();
            if (been_Blocked == false)
            {
                //the attack function will be launched
                Attack();
            }
            else
            {
                StartCoroutine(Block_Timer());
            }
        }

        if (is_Swinging == false) {
            //when the speed is above 1
            if (transform.forward.magnitude > 0) {
                //the will it play the run animation
                anim.Play(walk);
            }else{
                anim.Play(idle);
            }
        }
        //it will search for enemies that are dead
        for (int i = 0; i<enemies.Count; i++) {
            if(enemies[i].tag == "dead"){
                //when the soldier found a dead enemy, then it will delete it from the enemies list
                enemies.RemoveAt(i);
            }
        }
    }
   
    //here will the attack be launched
    void Attack(){
        //first it will look at the enemy before swinging
        transform.LookAt (new Vector3(GetClosestEnemy ().transform.position.x, transform.position.y, GetClosestEnemy().transform.position.z));
        //the agent will be stopped
        agent.Stop();
        //then if the soldier can swing will it swing
        if (can_Swing == true) {
            //it will call the swing function
            StartCoroutine(Swing ());
        }
    }
   
    IEnumerator Swing(){
        yield return new WaitForSeconds (0.4f);
        //it will play the attack animation
        anim.Play (attack);
        can_Swing = false;
        is_Swinging = true;
        //after 0.7 seconds
        yield return new WaitForSeconds (1.7f);
        //the is swinging is false
        is_Swinging = false;
        //the can swing is true
        can_Swing = true;
    }
   
    //here will the closest enemy be calculated
    private GameObject GetClosestEnemy(){
        //only when the player is alive, because it would be a waste for the pc
        if (enemies.Count > 0) {
            //it will now sort
            enemies.Sort (
                (unit1, unit2) =>
                (transform.position - unit1.transform.position).sqrMagnitude.CompareTo
                ((transform.position - unit2.transform.position).sqrMagnitude)
                );
            return enemies.First ();
        } else {
            return null;
        }
    }

    IEnumerator Block_Timer(){
    yield return new WaitForSeconds(2.0f);
    been_Blocked = false;
    }
}

Hello @briyan

Your script seems to be pretty good and when not noticing any performance issues I think it is ok.
If you do want to make your script a bit faster, dont check every enemy every frame in your brain function if they are dead. Simply create a function that get called when the enemy dies and that removes itself from the list of enemies.

Also in your attack function you have this:

Void Attack()
{
transform.LookAt(new Vector3(Get losestEnemy().transform.position.x, transform.position.y, getClosestEnemy().transform.position.z)
}

You dont have to get the closest player again because you already calculated it inside the brain function to calculate the distance. Create a seperate variable inside brain() that holds the position of the closest player. Make the attack() function accept a parameter vector3. Send the position of the closestplayer with it when you call Attack and use this position inside the lookat function.

This way you only calculate the closest player 1 time par frame, instead of 3 times.

Hope I made a clear explanation, if not I will try it on a computer instead of a tablet ;).

~Floris Weers