Example #1
0
    public IEnumerator MeleeChase(UnitHealth _target, Transform _targetPos)
    {
        try
        {
            if (_target == null)
            {
                SetBlocking(false);
                SetIncombat(false);
                hasTarget = false;
                Debug.Log("Lost Target");
                yield break;
            }
        }
        catch
        {
            Debug.LogError("No target on starup");
            CheckAgro();
        }


        target    = _target;
        targetPos = _targetPos;
        //   StartCoroutine(rotate(_targetPos));
        checks = 0;

        while (true)
        {
            try
            {
                navAgent.destination = _targetPos.position;
            }
            catch
            {
                //  Debug.LogError("Null Ref");
                SetBlocking(false);
                SetIncombat(false);
                hasTarget = false;
                CheckAgro();
                yield break;
            }

            if (_target == null)
            {
                SetBlocking(false);
                SetIncombat(false);
                hasTarget = false;
                Debug.Log("Lost Target");
                CheckAgro();
                yield break;
            }


            //    if (checks >= 30)
            //    {
            //        hasTarget = false;
            //        CheckAgro();
            //        yield break;
            //    }



            SetIncombat(true);
            //Debug.Log("Chase");
            hasTarget = true;


            yield return(new WaitForSeconds(checkTime));



            //start behavior demon melee
            if (CheckDist() <= meleeRange)
            {
                int random = (int)Random.Range(-0.5f, 7.4f);
                transform.LookAt(_targetPos);


                if (healthScript.currentHealth <= 20f)
                {
                    int rand = (int)Random.Range(0, 1);

                    if (rand == 0)
                    {
                        SetBlocking(true);
                    }
                    if (rand == 1)
                    {
                        StartCoroutine(OverWhelm(target));
                    }
                }

                if (random == 0 && canAttack == true && targetPos != null)
                {
                    Attack(_target);
                    RechargeAttackTimer(basicAttackCD);
                    yield return(new WaitForSeconds(0.5f));
                }
                else if (random == 1 && canAttack == true && targetPos != null)
                {
                    HeavyAttack(_target);
                    RechargeAttackTimer(heavyAttackCd);
                    yield return(new WaitForSeconds(0.5f));
                }
                else if (random == 2 && targetPos != null)
                {
                    SetBlocking(true);
                    healthScript.blocking = true;
                    yield return(new WaitForSeconds(1.0f));

                    if (targetPos != null)
                    {
                        unitAnimator.BlockAttack(target);
                    }
                    healthScript.blocking = false;
                    SetBlocking(false);
                    yield return(new WaitForSeconds(0.1f));
                }
                else if (random == 3 && targetPos != null)
                {
                    HeavyAttack(_target);
                    StartCoroutine(Strafe());
                    yield return(new WaitForSeconds(0.5f));
                }
                else if (random == 4 && targetPos != null)
                {
                    Attack(_target);
                    Dodge(_targetPos.position, 5f);
                    yield return(new WaitForSeconds(0.5f));
                }
                else if (target.blocking == true && random == 5 && targetPos != null && canAttack == true)
                {
                    try
                    {
                        Rush(_targetPos.position, 5f);
                    }
                    catch
                    {
                        CheckAgro();
                        yield break;
                    }
                    StartCoroutine(OverWhelm(target));
                    yield return(new WaitForSeconds(0.4f));

                    try
                    {
                        Rush(_targetPos.position, 5f);
                    }
                    catch
                    {
                        CheckAgro();
                        yield break;
                    }
                    yield return(new WaitForSeconds(0.4f));

                    try
                    {
                        Rush(_targetPos.position, 5f);
                    }
                    catch
                    {
                        CheckAgro();
                        yield break;
                    }
                }
                else
                {
                    Attack(_target);
                    RechargeAttackTimer(basicAttackCD);
                    yield return(new WaitForSeconds(0.5f));
                }
            }
            checks++;
        }
    }