// scans nearby entites and selects closest one and tracking target
    bool ScanTargets()
    {
        Profiler.BeginSample("ScanTargets");

        int layer = 1 << LayerMask.NameToLayer("Units");

        Collider[] colliders = Physics.OverlapCapsule(transform.position - new Vector3(0, 8, 0), transform.position + new Vector3(0, 8, 0), VisionRange, layer);

        float minDist = Mathf.Infinity;

        targetEnemy = null;

        foreach (Collider c in colliders)
        {
            UnitHealth enemy = c.GetComponent <UnitHealth>();
            if (enemy == null)
            {
                continue;
            }
            if (c.gameObject == gameObject)
            {
                continue;
            }
            if (enemy.Type != UnitHealth.UnitType.Player)
            {
                continue;
            }

            Collider col = enemy.GetComponent <Collider>();

            Vector3 pos = col ? col.bounds.center : transform.position;

            if (!IsVisible(pos))
            {
                continue;
            }

            float dist = (c.transform.position.SetY(0) - transform.position.SetY(0)).magnitude;

            if (dist < minDist)
            {
                minDist     = dist;
                targetEnemy = enemy;
            }
        }

        distToEnemy  = minDist;
        angleToEnemy = targetEnemy ? Mathf.Abs(Vector3.SignedAngle(transform.forward, (targetEnemy.transform.position - transform.position).normalized, Vector3.up)) : 0;

        Profiler.EndSample();

        return(targetEnemy != null);
    }
    public void Start()
    {
        unitName     = GetComponentInChildren <UnitName>();
        unitPortrait = GetComponentInChildren <UnitPortrait>();
        unitHealth   = GetComponentInChildren <UnitHealth>();
        unitStats    = GetComponentInChildren <UnitStats>();

        activeName     = unitName.GetComponent <Text>();
        healthSlider   = unitHealth.GetComponent <Slider>();
        activePortrait = unitPortrait.GetComponent <Image>();

        stat1Description = unitStats.GetComponentsInChildren <Text>().FirstOrDefault(s => s.text.Equals(stat1DescriptionText));
        stat2Description = unitStats.GetComponentsInChildren <Text>().FirstOrDefault(s => s.text.Equals(stat2DescriptionText));
        stat3Description = unitStats.GetComponentsInChildren <Text>().FirstOrDefault(s => s.text.Equals(stat3DescriptionText));
        stat4Description = unitStats.GetComponentsInChildren <Text>().FirstOrDefault(s => s.text.Equals(stat4DescriptionText));

        stat1Value = stat1Description.GetComponentsInChildren <Text>().FirstOrDefault(s => s.text.Equals("100"));
        stat2Value = stat2Description.GetComponentsInChildren <Text>().FirstOrDefault(s => s.text.Equals("100"));
        stat3Value = stat3Description.GetComponentsInChildren <Text>().FirstOrDefault(s => s.text.Equals("100"));
        stat4Value = stat4Description.GetComponentsInChildren <Text>().FirstOrDefault(s => s.text.Equals("100"));
    }