11.12 Unity 케릭터 공격모션 + 상대방 피격모션 구현하기
C#/실습 2019. 11. 12. 20:00예제:

코드:
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class App : MonoBehaviour
{
public Animation Anim1;
public Animation Anim2;
public Button btn;
private float elapsedTime1; //경과 시간
private float elapsedTime2;
private bool isAttackAnimationPlay1 = false;
private bool isAttackAnimationPlay2 = false;
private bool isAttackAnimationPlay3 = false;
private bool isAttacked = false;
private bool isHero2Alive = true;
private Hero hero1;
private Hero hero2;
private Sword sword;
// Start is called before the first frame update
void Start()
{
//버튼을 누르면 공격 애니메이션을 실행한다
this.btn.onClick.AddListener(() =>
{
Debug.Log("버튼을 눌렀습니다.");
isAttackAnimationPlay1 = true;
});
//AnimationState state1 = this.Anim1["attack_sword_01"];
//foreach (AnimationState state in Anim1)
//{
// if (state.name == "attack_sword_01")
// {
// }
//}
//this.Anim.Play("attack_sword_01");
//model 불러오기
var hero1Model = Instantiate(heroPrefab);
var hero2Model = Instantiate(heroPrefab);
GameObject heroGO1 = new GameObject("Hero1");
hero1 = heroGO1.AddComponent<Hero>();
hero1Model.transform.SetParent(heroGO1.transform, false);
hero1Model.transform.localPosition = Vector3.zero;
hero1Model.transform.localRotation = new Quaternion(0, 0, 0, 0);
hero1.model = hero1Model;
heroGO1.transform.position = Vector3.zero;
GameObject heroGO2 = new GameObject("Hero2");
hero2 = heroGO2.AddComponent<Hero>();
hero2Model.transform.SetParent(heroGO2.transform, false);
hero2Model.transform.localPosition = Vector3.zero;
hero2Model.transform.localRotation = new Quaternion(0, 0, 0, 0);
hero2.model = hero2Model;
heroGO2.transform.position = new Vector3(1, 0, 0);
GameObject swordGO = new GameObject("Sword");
sword = swordGO.AddComponent<Sword>();
}
// Update is called once per frame
//매 프레임 마다 호출됨(각 프레임 경과 타임은 다르다)
void Update()
{
if (isAttackAnimationPlay1 == true)
{
this.elapsedTime1 += Time.deltaTime;
//0.2665초때 타격이라고 출력하기
if (this.elapsedTime1 >= 0.2665f && isAttacked == false)
{
isAttacked = true;
this.elapsedTime2 = 0;
Debug.LogFormat("타격1! 시간: {0}", this.elapsedTime1);
}
if (this.elapsedTime1 >= 0.733f)
{
this.isAttackAnimationPlay1 = false;
this.elapsedTime1 = 0;
this.isAttacked = false;
this.isAttackAnimationPlay2 = true;
}
}
if (isAttackAnimationPlay2 == true)
{
this.elapsedTime1 += Time.deltaTime;
//0.5333초때 타격이라고 출력하기
if (this.elapsedTime1 >= 0.5333f && isAttacked == false)
{
isAttacked = true;
this.elapsedTime2 = 0;
Debug.LogFormat("타격2! 시간: {0}", this.elapsedTime1);
}
if (this.elapsedTime1 >= 0.8f)
{
this.isAttackAnimationPlay2 = false;
this.elapsedTime1 = 0;
this.isAttacked = false;
this.isAttackAnimationPlay3 = true;
}
}
if (isAttackAnimationPlay3 == true)
{
this.elapsedTime1 += Time.deltaTime;
//0.5333초때 타격이라고 출력하기
if (this.elapsedTime1 >= 0.5333f && isAttacked == false)
{
isAttacked = true;
this.elapsedTime2 = 0;
Debug.LogFormat("타격3! 시간: {0}", this.elapsedTime1);
}
if (this.elapsedTime1 >= 0.8f)
{
this.isAttackAnimationPlay3 = false;
this.elapsedTime1 = 0;
this.isAttacked = false;
}
}
this.elapsedTime2 += Time.deltaTime;
if (isAttacked == true && isHero2Alive == true)
{
}
{
isHero2Alive = false;
}
if (this.elapsedTime2 >= 0.8f && isHero2Alive == true)
{
this.elapsedTime2 = 0;
}
{
isHero2Alive = true;
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hero : MonoBehaviour
{
public int hp;
public GameObject model;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void GetHit(int damage)
{
if(this.hp - damage <= 0)
{
this.hp = 0;
}
else
{
this.hp -= damage;
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sword : MonoBehaviour
{
public int damage;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
'C# > 실습' 카테고리의 다른 글
11.26 Hero와 Monster의 App에 의한 수동적 싸움 (delegate) (0) | 2019.11.26 |
---|---|
11.13 unity 유닛 동적생성, 유닛 이동 및 사정거리 내 진입시 공격하기 (0) | 2019.11.13 |
Unity 11.11 케릭터 동적 생성하고 무기 장착및 교체하기 (0) | 2019.11.11 |
11.08 Unity 동적 캐릭터 생성 후 사거리에 따라 공격기능 on/off (0) | 2019.11.08 |
11.07 Unity monster객체에 역직렬화 데이터와 모델 데이터 넣기. (0) | 2019.11.07 |