카테고리 없음
유니티VR - RobotHunter EnemyCtrl.cs
hololol
2019. 7. 24. 09:41
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class EnemyCtrl : MonoBehaviour { //플레이어를 향해서 이동하고 남은거리가 2라면 멈춘다 ==> 애니메이션을 idle 상태로. //적 캐릭터가 공격받으면 발생할 이펙트를 저장하는 변수 public GameObject HitEffect; //적 캐릭터의 HP를 저장하는 변수 private int HP; Transform player; NavMeshAgent agent; float distance; Animator anim; //공격 중 상태를 저장하는 변수 private bool isAttack; private void Start() { //시작 HP를 100으로 설정 HP = 100; //하이라키 뷰에서 Player 이름을 가진 오브젝트를 찾아서 Transform 정보를 얻는다. player = GameObject.Find("Player").GetComponent<Transform>(); //추적을 위한 NavMeshAgent 컴포넌트를 저장 agent = GetComponent<NavMeshAgent>(); //애니메이션 변경을 위한 Animator 컴포넌트를 저장 anim = GetComponent<Animator>(); //플레이어가 나의 목적지, 나를 향해 달려온다, 플레이어의 위치정보를 넣어준다. agent.destination = player.position; } private void Update() { //너의 위치와 나의 위치를 뺀다 distance = Vector3.Distance(player.position, transform.position); //남은 거리가 2이하이면 Idle(bool 파라미터) 애니메이션 실행 if (distance < 2) { agent.isStopped = true; if (isAttack == false) { anim.SetBool("Idle", true); StartCoroutine(Attack()); } } } IEnumerator Attack() { isAttack = true; yield return new WaitForSeconds(0.5f); anim.SetBool("Attack", true); yield return new WaitForSeconds(0.5f); isAttack = false; anim.SetBool("Attack", false); } //충돌 함수 private void OnCollisionEnter(Collision coll) { //충돌한 오브젝트의 Tag가 Bullet인경우 if (coll.gameObject.CompareTag("Bullet")) { //충돌한 위치에 Hit 이펙트를 생성한다 GameObject effect = Instantiate(HitEffect, coll.transform.position, coll.transform.rotation); //충돌한 오브젝트를 제거한다. Destroy(coll.gameObject); //HitEffect를 2초후에 제거한다 Destroy(HitEffect, 2.0f); //HP를 10깎는다 HP -= 10; //HP가 0 이하이면 if (HP <= 0) { //자기자신을 제거한다 Destroy(gameObject); } } } } | cs |