Esempio n. 1
0
    public IEnumerator RangedChase(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;
        SetIncombat(true);

        try
        {
            navAgent.destination = GetPositionDistanceFromPoint(_targetPos.position, rangedSkirmishDist);
        }
        catch
        {
            SetIncombat(false);
            hasTarget = false;
            CheckAgro();
            yield break;
        }

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


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



            //   SetIncombat(true);
            hasTarget = true;


            yield return(new WaitForSeconds(checkTime));

            try
            {
                dist = CheckDistFromtarget(_targetPos);
            }
            catch
            {
                hasTarget = false;
                CheckAgro();
                yield break;
            }

            int random = (int)Random.Range(0, 5.4f);


            //Debug.Log(dist);
            if (dist <= meleeRange)
            {
                if (random == 0 | random == 1)
                {
                    Attack(target);
                }
                else if (random == 2 | random == 3)
                {
                    StartCoroutine(DodgeBack(_targetPos.position));
                }
                else if (random == 4 | random == 5)
                {
                    navAgent.destination = GetPositionDistanceFromPoint(_targetPos.position, rangedSkirmishDist);
                }
            }
            else if (dist <= rangedRange)
            {
                if (random == 0 && canAttack == true)
                {
                    transform.LookAt(_targetPos);
                    // DodgeBack(_targetPos.position);
                    unitAnimator.randomShootAttackLight(LightProjectile, targetPos, target, shootPosition);
                    RechargeAttackTimer(lightProjectileCooldown);
                    yield return(new WaitForSeconds(lightProjectileCooldown));
                }
                else if (random == 1 && canMove == true)
                {
                    navAgent.destination = GetPositionDistanceFromPoint(_targetPos.position, rangedSkirmishDist);
                }
                else if (random == 2 && canMove == true)
                {
                    navAgent.destination = GetPositionDistanceFromPoint(_targetPos.position, rangedSkirmishDist);
                }
                else if (random == 3 && canMove == true)
                {
                    StartCoroutine(Strafe());
                }
                else if (random == 4 && canAttack == true)
                {
                    transform.LookAt(_targetPos);
                    // DodgeBack(_targetPos.position);
                    unitAnimator.randomShootAttackLight(LightProjectile, targetPos, target, shootPosition);
                    RechargeAttackTimer(lightProjectileCooldown);
                    yield return(new WaitForSeconds(1f));

                    transform.LookAt(_targetPos);
                    // DodgeBack(_targetPos.position);
                    unitAnimator.randomShootAttackLight(LightProjectile, targetPos, target, shootPosition);
                    RechargeAttackTimer(lightProjectileCooldown);
                    yield return(new WaitForSeconds(0.4f));
                }
                else if (random == 5 && canAttack == true)
                {
                    Debug.Log("Heavy Atk");
                    transform.LookAt(_targetPos);
                    // DodgeBack(_targetPos.position);
                    unitAnimator.randomShootAttackHeavy(HeavyProjectile, targetPos, target, shootPosition);
                    RechargeAttackTimer(heavyProjectileCooldown);
                    yield return(new WaitForSeconds(heavyProjectileCooldown));
                }
            }
            checks++;
        }
    }