Example #1
0
    private void TowerAttack(Vector3 pos)
    {
        if (towerList.Count != 0)
        {
            // 가까운적 타겟팅
            float currentDist = 0f;

            for (int i = 0; i < towerList.Count; i++)
            {
                currentDist = Vector3.Distance(pos, towerList[i].transform.position);

                if (currentDist < towerDistance)
                {
                    isTarget = true;
                }
            }
        }

        if (isTarget)
        {
            // 최단 거리에 있는 유닛을 바라 보게 한다.

            Vector3 dirToTarget = towerList[0].transform.position - this.transform.position;
            Vector3 look        = Vector3.Slerp(this.transform.forward, dirToTarget.normalized, Time.deltaTime);
            this.transform.rotation = Quaternion.LookRotation(look, Vector3.up);


            //transform.LookAt(new Vector3(towerList[0].transform.position.x,
            //                             pos.y,
            //                             towerList[0].transform.position.z));
            aniStaus = AniStatus.ATTACK;
        }
    }
 private void OnEnable()
 {
     fillAmountHp = 100 / enemy_Hp;
     moneyParticle.Stop(true);
     agent.SetDestination(tf_Destination.position);
     aniStaus = AniStatus.WALK;
 }
    protected void AnimStatus(AniStatus _animStatus)
    {
        switch (_animStatus)
        {
        case AniStatus.IDLE:
            animator.SetBool("isWalk", false);
            animator.SetBool("isAttack", false);

            break;

        case AniStatus.ATTACK:
            animator.SetBool("isWalk", false);
            animator.SetBool("isAttack", true);

            break;

        case AniStatus.DIE:
            animator.SetBool("isDeath", true);
            StartCoroutine("DeathUnit");
            break;

        case AniStatus.WALK:
            animator.SetBool("isWalk", true);
            break;
        }
    }
Example #4
0
 private void Awake()
 {
     animator       = GetComponent <Animator>();
     aniStaus       = AniStatus.IDLE;
     unitController = GetComponent <UnitController>();
     unitMeshList   = GetComponentsInChildren <SkinnedMeshRenderer>();
     layerMask      = 1 << 10;
     // parentObject = GetComponentInParent<GameObject>();
 }
    // 애니메이션 status를 바꾸는 상황
    protected void StatusChange(Vector3 _pos)
    {
        // 주변에 있는 모든 unit 적을 추출하여 배열에 저장
        Collider[] colls       = Physics.OverlapSphere(_pos, 3f, 1 << 9);
        Collider[] CheckAround = Physics.OverlapSphere(_pos, 3f, 1 << 9);

        for (int i = 0; i < colls.Length; ++i)
        {
            // Unit 콜라이더를 담고 잇는 녀석에게 "AttackUnit"이라는 함수를 가동시켜라.
            if (enemy_Hp > 0)
            {
                colls[i].SendMessage("UnitStatus");
                colls[i].SendMessage("UnitAnimStatus");
                //  aniStaus = AniStatus.ATTACK;
            }
            if (colls[i].GetComponent <UnitBase>().Unit_Hp <= 0f)
            {
                enemyUnit.Remove(colls[i].gameObject);
                aniStaus = AniStatus.IDLE;
            }
        }

        for (int i = 0; i < CheckAround.Length; ++i)
        {
            if (enemyUnit.Count == 0)
            {
                enemyUnit.Add(CheckAround[i].gameObject);
            }

            for (int j = 0; j < enemyUnit.Count; j++)
            {
                if (enemyUnit[j].name != CheckAround[i].name)
                {
                    enemyUnit.Add(CheckAround[i].gameObject);
                }
            }

            target   = CheckAround[0].gameObject;
            isTarget = true;
            //}
        }
        if (CheckAround.Length == 0)
        {
            enemyUnit.Clear();
            isTarget = false;
        }
    }
    private void OnTriggerEnter(Collider other)
    {
        // enemy의 hp가 0 이상일때만 공격 신호를 보낸다.
        if (enemy_Hp > 0)
        {
            if (other.gameObject.tag == "PlayerUnit")
            {
                // Unit 콜라이더를 담고 잇는 녀석에게 "AttackUnit"이라는 함수를 가동시켜라.
                other.SendMessage("UnitStatus");
                other.SendMessage("UnitAnimStatus");
                aniStaus = AniStatus.ATTACK;

                Debug.Log("playerUnit을 발견 했다.");
            }
        }

        if (other.gameObject.tag == "PlayerTower")
        {
            aniStaus = AniStatus.ATTACK;
        }
    }
Example #7
0
 public void UnitStatus()
 {
     aniStaus = AniStatus.ATTACK;
 }
Example #8
0
    //private void OnDrawGizmos()
    //{
    //    if (getATarget)
    //    {
    //        for (int i = 0; i < EnemyList.Count; i++)
    //        {
    //            RaycastHit hit;
    //            bool isHit = Physics.Raycast(transform.position, EnemyList[i].transform.position - transform.position,
    //                                         out hit, 20f, layerMask);

    //            // 수정할 부분이 많음
    //            if (isHit && hit.transform.CompareTag("EnemyUnit"))
    //            {
    //                Gizmos.color = Color.green;
    //            }
    //            else
    //            {
    //                Gizmos.color = Color.red;
    //            }
    //            Gizmos.DrawRay(transform.position, EnemyList[i].transform.position - transform.position);
    //        }
    //    }

    //    for (int i = 0; i < towerList.Count; i++)
    //    {
    //        RaycastHit hit;
    //        bool isHit = Physics.Raycast(transform.position, towerList[i].transform.position - transform.position,
    //                                     out hit, 20f, 1 << 12);

    //        // 수정할 부분이 많음
    //        if (isHit && hit.transform.CompareTag("Cannon") || isHit && hit.transform.CompareTag("Balista"))
    //        {
    //            Gizmos.color = Color.green;
    //        }
    //        else
    //        {
    //            Gizmos.color = Color.red;
    //        }
    //        Gizmos.DrawRay(transform.position, EnemyList[i].transform.position - transform.position);
    //    }
    //}



    private void FixedUpdate()
    {
        RotateLookAt(this.transform.position);
        NearEnemy(transform.position);

        if (unitController.isWalk == false && aniStaus != AniStatus.ATTACK)
        {
            aniStaus = AniStatus.IDLE;
        }
        if (unitController.isWalk == true)
        {
            aniStaus = AniStatus.WALK;
        }

        if (Unit_Hp <= 0 && alphaValue > 0)
        {
            aniStaus = AniStatus.DIE;
            this.gameObject.GetComponent <UnitController>().SetSelected(false);

            if (isAlpha == true)
            {
                alphaValue -= Time.deltaTime * 2f;
                foreach (SkinnedMeshRenderer mesh in unitMeshList)
                {
                    mesh.material.color = new Color(mesh.material.color.r, mesh.material.color.g, mesh.material.color.b, alphaValue);
                }
            }
        }
        if (alphaValue <= 0)
        {
            this.gameObject.SetActive(false);

            if (isDeath == false)
            {
                GameManager.instance.UnitCount -= 1;
                isDeath = true;
            }
            Destroy(transform.parent.gameObject);
        }

        UnitAnimStatus();


        // 가까운적 타겟팅
        if (EnemyList.Count != 0)
        {
            currentDist    = 0f;
            closeDistIndex = 0;
            targetIndex   -= 1;

            for (int i = 0; i < EnemyList.Count; i++)
            {
                currentDist = Vector3.Distance(transform.position, EnemyList[i].transform.position);

                RaycastHit hit;
                bool       isHit = Physics.Raycast(transform.position, EnemyList[i].transform.position - transform.position,
                                                   out hit, 20f, layerMask);

                if (isHit && hit.transform.CompareTag("EnemyUnit"))
                {
                    if (targetDist >= currentDist)
                    {
                        targetIndex = i;
                        targetDist  = currentDist;
                    }
                }

                if (closeDist >= currentDist)
                {
                    closeDistIndex = i;
                    closeDist      = currentDist;
                }
            }

            if (targetIndex == -1)
            {
                targetIndex = closeDistIndex;
            }

            closeDist  = 100f;
            targetDist = 100f;
            getATarget = true;
        }

        if (getATarget)
        {
            // 최단 거리에 있는 유닛을 바라 보게 한다.
            //Vector3 dirToTarget = EnemyList[targetIndex].transform.position - this.transform.position;
            //Vector3 look = Vector3.Slerp(this.transform.forward, dirToTarget.normalized, Time.deltaTime);
            //  this.transform.rotation = Quaternion.LookRotation(look, Vector3.up);

            transform.LookAt(new Vector3(EnemyList[targetIndex].transform.position.x,
                                         transform.position.y, EnemyList[targetIndex].transform.position.z));
        }

        if (towerList.Count != 0)
        {
            if (towerList[0].tag == "Cannon")
            {
                if (towerList[0].transform.parent.Find("SCALED").GetComponentInChildren <TowerTargetingRE>().tower_Hp <= 0)
                {
                    towerList.Clear();
                    aniStaus = AniStatus.IDLE;
                }
            }

            if (towerList[0].tag == "Balista")
            {
                if (towerList[0].transform.parent.Find("Balista_Head").GetComponent <BalistaTargetingRE>().tower_Hp <= 0)
                {
                    towerList.Clear();
                    aniStaus = AniStatus.IDLE;
                }
            }
        }

        if (EnemyList.Count == 0)
        {
            aniStaus = AniStatus.IDLE;
        }

        NearTower(transform.position);
        TowerAttack(transform.position);
    }
    protected void FixedUpdate()
    {
        StatusChange(this.transform.position);
        AnimStatus(aniStaus);

        if (enemyUnit.Count != 0)
        {
            agent.isStopped = true;
            //aniStaus = AniStatus.IDLE;
        }

        if (enemy_Hp <= 0)
        {
            aniStaus = AniStatus.DIE;
            hpSlider.gameObject.SetActive(false);

            if (isAlpha == true)
            {
                alphaValue -= Time.deltaTime * 02f;
                foreach (SkinnedMeshRenderer mesh in enemyMeshList)
                {
                    mesh.material.color = new Color(mesh.material.color.r, mesh.material.color.g, mesh.material.color.b, alphaValue);
                }
            }
        }

        if (alphaValue <= 0 && isAlphaDone == false)
        {
            moneyParticle.Play(true);
            isAlphaDone = true;
        }
        if (isAlphaDone == true)
        {
            StartCoroutine("MoneyEffect");
        }

        // 만약 타겟이 생기면 타겟한테 향하자
        if (enemy_Hp > 0)
        {
            if (isTarget)
            {
                float moveSpeed = 2.0f;

                transform.LookAt(target.transform);

                Vector3 dirToTarget = enemyUnit[0].transform.position - this.transform.position;
                Vector3 look        = Vector3.Slerp(this.transform.forward, dirToTarget.normalized, Time.deltaTime);
                this.transform.rotation = Quaternion.LookRotation(look, Vector3.up);

                //if (Vector3.Distance(target.transform.position, transform.position) > 13f)
                //{
                //    aniStaus = AniStatus.WALK;
                //    transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
                //}

                //if (Vector3.Distance(target.transform.position, transform.position) <= 13f)
                //{
                aniStaus = AniStatus.ATTACK;
                //}
            }
        }
        else
        {
            isTarget = false;
        }

        if (target != null)
        {
            if (target.GetComponent <UnitBase>().Unit_Hp <= 0)
            {
                aniStaus = AniStatus.IDLE;
            }
        }

        if (enemyUnit.Count == 0)
        {
            agent.isStopped = false;
            aniStaus        = AniStatus.WALK;
        }
    }