[Unity]코루틴 사용하여 캐릭터 이동 및 공격모션
Unity/과제2019. 4. 17. 01:08
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 | using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Character : MonoBehaviour { private Animation anim; private Character target; private float attackRange = 0.5f; private bool isStart; // Start is called before the first frame update private void Awake() { this.anim = this.gameObject.GetComponent<Animation>(); } void Start() { } // Update is called once per frame void Update() { } public void Position(Vector3 position) { this.transform.position = position; } public IEnumerator Move(Vector3 tPos) { this.anim.Play("run@loop"); this.isStart = true; while(true) { if (this.isStart == true) { this.transform.LookAt(tPos); var dir = (tPos - this.transform.position).normalized; var speed = 1; this.transform.position += dir * speed * Time.deltaTime; var distance = Vector3.Distance(tPos, this.transform.position); if (attackRange > distance) { this.anim.Play("idle@loop"); this.isStart = false; if (distance <= 0.1f && this.isStart == true) { anim.Play("idle@loop"); Debug.Log("이동완료"); break; } } } yield return null; } } public IEnumerator Attack(Character target) { this.anim.Play("idle@loop"); this.target = target; while (true) { var distance = Vector3.Distance(this.transform.position, target.transform.position); if (attackRange > distance) { this.transform.LookAt(target.transform.position); anim.Play("attack_sword_01"); break; } yield return null; } } } | cs |
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 | using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class App : MonoBehaviour { public Character hero; public Character monster; // Start is called before the first frame update void Start() { StartCoroutine(this.hero.Move(new Vector3(5, 0, 5))); this.monster.Position(new Vector3(5, 0, 5)); StartCoroutine(this.monster.Attack(this.hero)); } // Update is called once per frame void Update() { } } | cs |
'Unity > 과제' 카테고리의 다른 글
unity 지정된 몬스터 공격 및 대상 삭제 (0) | 2019.04.24 |
---|---|
타이틀씬까지 로드 (0) | 2019.04.18 |
FadIn,FadOut 코드 (0) | 2019.04.17 |
유니티 게임시작화면관리하기 (0) | 2019.04.16 |
유니티 캐릭터생성 및 위치지정 (0) | 2019.04.15 |