C#/수업내용
20200526 / 코루틴(Coroutine)
NexTin
2020. 5. 26. 18:28
코루틴을 어떻게 쓸지는 감이 잡히는듯함.
주의할건 탈출구 break 잘 적을것
애니메이션 재생시키는건 더 연습할 필요가 있음..
WaitForSeconds를 사용하여 시간 지연을 도입할 수도 있습니다.
코루틴 금일 코드 >>
더보기
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
97
98
99
100
101
102
103
104
105
106
|
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.Events;
public class Hero : MonoBehaviour
{
public GameObject model;
public Transform targetPoint;
public Hero target;
public bool isEnemy;
private Animation anim;
private UnityAction onMoveComplete;
private UnityAction onAttackComplete;
private UnityAction onHitCompete;
// Start is called before the first frame update
void Start()
{
this.anim = this.model.GetComponent<Animation>();
this.onHitCompete = () => { this.Idle(); };
if (isEnemy) return;
this.target = this.targetPoint.GetComponent<Hero>();
Debug.LogFormat("target : {0}", this.target);
this.onAttackComplete = () => {
StartCoroutine(this.Attack());
};
this.onMoveComplete = () => {
StartCoroutine(this.Attack());
};
this.StartCoroutine(this.Move());
}
private void Idle()
{
this.anim.Play("idle@loop");
}
IEnumerator Hit()
{
this.anim.Play("damage");
yield return new WaitForSeconds(this.anim["damage"].length);
this.onHitCompete();
}
IEnumerator Attack()
{
yield return new WaitForSeconds(.5f);
var length = this.anim["attack_sword_01"].length;
var isImpact = false;
this.anim.Play("attack_sword_01");
float elaspedTime = 0;
while (true)
{
elaspedTime += Time.deltaTime;
if (elaspedTime >= 0.2665f)
{
if (!isImpact)
{
isImpact = true;
Debug.Log("Impact");
StartCoroutine(this.target.Hit());
}
}
if (elaspedTime >= length)
{
break;
}
yield return null;
}
this.onAttackComplete();
}
IEnumerator Move()
{
this.anim.Play("run@loop");
while (true)
{
this.transform.Translate(Vector3.forward * 1f * Time.deltaTime);
var dis = Vector3.Distance(this.targetPoint.position, this.transform.position);
if(dis <= 0.5f)
{
break;
}
yield return null;
}
Debug.Log("이동완료");
this.onMoveComplete();
}
}
|
cs |