//Try to move unit to target
        void Attack(UnitInstance unit, int targetGuid)
        {
            unit.navAgent.enabled = true;
            unit.isMoving         = true;

            unit.anim.SetBool("Walk", true);

            unit.isAttacking = true;
            unit.target      = targetGuid;

            if (unit.pathFinding != null)
            {
                StopCoroutine(unit.pathFinding);
            }
            unit.pathFinding = MoveAttackingUnit(unit);
            StartCoroutine(unit.pathFinding);
        }
        //Load unit info panel (bottom left panel)
        public void LoadUnitUI(int unitGuid, int player)
        {
            UnitInstance currentUnit = deployedUnits[unitGuid];

            selectedUnitText.text = currentUnit.unitName;

            selectedUnitIcon.sprite = units[currentUnit.id].icon[currentUnit.owner];

            selectedUnitHP.text = currentUnit.currentHP + "/" + currentUnit.maxHP;

            selectedUnitHP.transform.parent.GetChild(0).localScale = new Vector3((currentUnit.currentHP / (float)currentUnit.maxHP), 1, 1);

            //Check if Unit is Barracks
            if (!currentUnit.canAttack)
            {
                attackUI.SetActive(false);
                defenseUI.SetActive(false);
                LoadDeployableUnits(currentUnit.owner);

                if (players[player].queueUnits != null)
                {
                    if (players[player].queueUnits.Count >= 1)
                    {
                        deployInfo.SetActive(true);
                    }
                    else
                    {
                        deployInfo.SetActive(false);
                    }
                }
                else
                {
                    deployInfo.SetActive(false);
                }
            }
            else
            {
                attackUI.SetActive(true);
                attackText.text = currentUnit.attack.ToString();
                defenseUI.SetActive(true);
                defenseText.text = currentUnit.defense.ToString();
                deployMenu[0].SetActive(false);
                deployMenu[1].SetActive(false);
                deployInfo.SetActive(false);
            }
        }
        //Try to reach unit move destination
        IEnumerator CheckDestination(UnitInstance unitToMove, bool buildingHit)
        {
            unitToMove.target = -1;
            unitToMove.anim.SetBool("Attack", false);

            if (unitToMove.isAttacking)
            {
                unitToMove.anim.SetTrigger("Quit");
            }
            unitToMove.isAttacking = false;

            float stopDistance = 0.5f;

            if (selectedUnits.Count > 1)
            {
                stopDistance = 1f;
            }

            if (buildingHit)
            {
                stopDistance = stopDistance + 0.5f;
            }

            while (Vector3.Distance(unitToMove.destination, unitToMove.unitObject.transform.position) > stopDistance)
            {
                yield return(0);
            }

            unitToMove.anim.SetBool("Walk", false);

            unitToMove.isMoving = false;

            unitToMove.navAgent.enabled = false;

            yield return(new WaitForSeconds(0.15f));

            if (flagPath != null)
            {
                if (selectedUnit == unitToMove.guid)
                {
                    Destroy(flagPath);
                }
            }
        }
        //Create unit in barracks
        public void SpawnUnit(int unitId, int player)
        {
            GameObject newObject = null;

            deployedUnits.Add(new UnitInstance(deployedUnits.Count, player, newObject = Instantiate(units[unitId].unitPrefab[player], barracks[player].transform.position + (barracks[player].transform.forward * 3f), Quaternion.identity), newObject.AddComponent <NavMeshAgent>(), newObject.GetComponent <Animator>(), units[unitId]));

            UnitInstance newUnit = deployedUnits[deployedUnits.Count - 1];

            MeleeWarriorUnitScript unit_mWUS = newObject.AddComponent <KevinIglesias.MeleeWarriorUnitScript>();

            unit_mWUS.coreScript = this;

            unit_mWUS.guid  = deployedUnits.Count - 1;
            unit_mWUS.id    = unitId;
            unit_mWUS.owner = player;

            newUnit.navAgent.baseOffset            = -0.04f;
            newUnit.navAgent.radius                = 0.075f;
            newUnit.navAgent.speed                 = 2.75f;
            newUnit.navAgent.acceleration          = 60f;
            newUnit.navAgent.stoppingDistance      = 0.5f;
            newUnit.navAgent.obstacleAvoidanceType = ObstacleAvoidanceType.GoodQualityObstacleAvoidance;
        }
        //Check if target position is reached and attack if so
        IEnumerator MoveAttackingUnit(UnitInstance attackerUnit)
        {
            while (attackerUnit.isAttacking)
            {
                if (deployedUnits[attackerUnit.target].canAttack)
                {
                    if (Vector3.Distance(deployedUnits[attackerUnit.target].unitObject.transform.position, attackerUnit.unitObject.transform.position) > attackerUnit.attackRange)
                    {
                        if (!attackerUnit.anim.GetCurrentAnimatorStateInfo(0).IsName("Attack 01") && !attackerUnit.anim.GetCurrentAnimatorStateInfo(0).IsName("Attack 02"))
                        {
                            LookAtClamp(attackerUnit.unitObject.transform, deployedUnits[attackerUnit.target].unitObject.transform.position);
                            attackerUnit.isMoving = true;
                            attackerUnit.anim.SetBool("Walk", true);
                            attackerUnit.navAgent.enabled = true;

                            Vector3 closestPoint = deployedUnits[attackerUnit.target].unitObject.GetComponent <Collider>().ClosestPointOnBounds(attackerUnit.unitObject.transform.position);

                            attackerUnit.navAgent.SetDestination(new Vector3(closestPoint.x, 0, closestPoint.z));
                        }
                        else
                        {
                            attackerUnit.anim.SetBool("Attack", false);
                        }
                    }
                    else
                    {
                        attackerUnit.isMoving = false;
                        LookAtClamp(attackerUnit.unitObject.transform, deployedUnits[attackerUnit.target].unitObject.transform.position);
                        attackerUnit.navAgent.enabled = false;
                        attackerUnit.anim.SetBool("Walk", false);
                        attackerUnit.anim.SetBool("Attack", true);

                        //Counterattack
                        if (!deployedUnits[attackerUnit.target].isMoving && !deployedUnits[attackerUnit.target].isAttacking)
                        {
                            yield return(new WaitForSeconds(0.1f));

                            Attack(deployedUnits[attackerUnit.target], attackerUnit.guid);
                        }
                    }
                }
                else
                {
                    //Find Closest Point to Building
                    Vector3 heading = deployedUnits[attackerUnit.target].unitObject.transform.position - attackerUnit.unitObject.transform.position;

                    Collider c = deployedUnits[attackerUnit.target].unitObject.GetComponent <Collider>();

                    Vector3 buildingClosestPoint = c.ClosestPoint(attackerUnit.unitObject.transform.position);

                    if (Vector3.Distance(buildingClosestPoint, attackerUnit.unitObject.transform.position) > (attackerUnit.attackRange * 0.75f))
                    {
                        if (!attackerUnit.anim.GetCurrentAnimatorStateInfo(0).IsName("Attack 01") && !attackerUnit.anim.GetCurrentAnimatorStateInfo(0).IsName("Attack 02"))
                        {
                            LookAtClamp(attackerUnit.unitObject.transform, deployedUnits[attackerUnit.target].unitObject.transform.position);
                            attackerUnit.isMoving = true;
                            attackerUnit.anim.SetBool("Walk", true);
                            attackerUnit.navAgent.enabled = true;


                            attackerUnit.navAgent.SetDestination(new Vector3(buildingClosestPoint.x, 0, buildingClosestPoint.z));
                        }
                        else
                        {
                            attackerUnit.anim.SetBool("Attack", false);
                        }
                    }
                    else
                    {
                        attackerUnit.isMoving = false;
                        LookAtClamp(attackerUnit.unitObject.transform, deployedUnits[attackerUnit.target].unitObject.transform.position);
                        attackerUnit.navAgent.enabled = false;
                        attackerUnit.anim.SetBool("Walk", false);
                        attackerUnit.anim.SetBool("Attack", true);
                    }
                }
                yield return(0);
            }
        }
        //Select clicked unit
        public void SelectUnit(int unitGuid)
        {
            if (flagPath != null)
            {
                Destroy(flagPath);
            }

            UnitInstance currentUnit = deployedUnits[unitGuid];

            currentUnit.selected = true;

            if (attackCo != null)
            {
                if (attackCOguid != -1)
                {
                    if (!deployedUnits[attackCOguid].selected)
                    {
                        deployedUnits[attackCOguid].selectionObject.SetActive(false);
                    }
                    deployedUnits[attackCOguid].selectionObject.GetComponent <Renderer>().material.color = Color.white;
                }
                StopCoroutine(attackCo);
            }

            if (currentUnit.selectionObject == null)
            {
                currentUnit.selectionObject = Instantiate(selectionPrefab);

                currentUnit.selectionObject.GetComponent <Renderer>().material.color = Color.white;

                currentUnit.selectionObject.transform.SetParent(currentUnit.unitObject.transform);
                currentUnit.selectionObject.transform.localScale       = new Vector3(units[currentUnit.id].selectionSize, units[currentUnit.id].selectionSize, units[currentUnit.id].selectionSize);
                currentUnit.selectionObject.transform.localPosition    = new Vector3(0, 0.05f, 0);
                currentUnit.selectionObject.transform.localEulerAngles = Vector3.zero;
                currentUnit.selectionObject.SetActive(true);
            }
            else
            {
                currentUnit.selectionObject.SetActive(true);
            }

            if (currentUnit.HPObject == null)
            {
                currentUnit.HPObject = Instantiate(HPPrefab);
                currentUnit.HPObject.transform.SetParent(currentUnit.unitObject.transform);
                currentUnit.HPObject.transform.localPosition = new Vector3(0, units[currentUnit.id].hpBarHeight, 0);
                currentUnit.HPObject.SetActive(true);
            }
            else
            {
                currentUnit.HPObject.SetActive(true);
            }

            if (currentUnit.isMoving)
            {
                if (!currentUnit.isAttacking)
                {
                    CreateFlagPath(currentUnit.destination, currentUnit.owner);
                }
            }
        }