示例#1
0
    IEnumerator DefendingAttack(GameObject attackTarget)
    {
        target = attackTarget.GetComponent <AttackTarget>();

        while (target.GetHealth() > 0)
        {
            Vector3 closestToTarget = GetComponent <Collider>().ClosestPoint(attackTarget.transform.position);
            Vector3 closestToThis   = attackTarget.GetComponent <Collider>().ClosestPoint(transform.position);

            if (Vector3.Distance(closestToThis, closestToTarget) < 50 && Vector3.Distance(closestToThis, closestToTarget) > weapon.GetRange())
            {
                animator.SetBool("Attacking", false);

                if (gameObject.GetComponent <IndividualMovement>().moving == false)
                {
                    GetComponent <IndividualMovement>().MoveTo(new Destination(attackTarget), null, true);
                }

                yield return(new WaitForSeconds(0.05f));
            }

            else if (Vector3.Distance(closestToThis, closestToTarget) > 50)
            {
                ChangeState(AttackUnitState.DEFENDING);
            }

            else
            {
                animator.SetBool("Attacking", true);
                target.TakeDamage(weapon.GetDamage());

                Vector3 dir = (attackTarget.transform.position - transform.position).normalized;

                transform.rotation = Quaternion.LookRotation(new Vector3(dir.x, 0, dir.z), Vector3.up);

                if (target.GetHealth() <= 0)
                {
                    ChangeState(AttackUnitState.DEFENDING);
                }

                yield return(new WaitForSeconds(1.0f / weapon.GetFiringRate()));
            }
        }
        ChangeState(AttackUnitState.DEFENDING);
        yield return(null);
    }
示例#2
0
    IEnumerator EnemyAttack(GameObject attackTarget)
    {
        AttackTarget target = attackTarget.GetComponent <AttackTarget>();

        while (target.GetHealth() > 0)
        {
            Vector3 closestToTarget = GetComponent <Collider>().ClosestPoint(attackTarget.transform.position);
            Vector3 closestToThis   = attackTarget.GetComponent <Collider>().ClosestPoint(transform.position);

            if (Vector3.Distance(closestToThis, closestToTarget) > attackRange)
            {
                animator.SetBool("Attacking", false);

                if (gameObject.GetComponent <IndividualMovement>().moving == false)
                {
                    GetComponent <IndividualMovement>().MoveTo(new Destination(attackTarget), null, true);
                }

                yield return(new WaitForSeconds(0.05f));
            }
            else
            {
                animator.SetBool("Attacking", true);
                target.TakeDamage(attackDamage);

                if (target.GetHealth() <= 0)
                {
                    ChangeEnemyState(EnemyState.RANDOM_MOVING);
                }

                Vector3 dir = (attackTarget.transform.position - transform.position).normalized;

                transform.rotation = Quaternion.LookRotation(new Vector3(dir.x, 0, dir.z), Vector3.up);

                yield return(new WaitForSeconds(1.0f / attackRate));
            }
        }
        ChangeEnemyState(EnemyState.RANDOM_MOVING);
        yield return(null);
    }