'Unity 케릭터 공격하기'에 해당되는 글 1건

  1. 2019.11.12 11.12 Unity 케릭터 공격모션 + 상대방 피격모션 구현하기

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;
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("버튼을 눌렀습니다.");
            this.Anim1.Play("attack_sword_01");
            isAttackAnimationPlay1 = true;
 
        });
        //AnimationState state1 = this.Anim1["attack_sword_01"];
        //foreach (AnimationState state in Anim1)
        //{
        //    if (state.name == "attack_sword_01")
        //    {
        //        Debug.Log(state);
        //    }
        //}
        //this.Anim.Play("attack_sword_01");
 
        //model 불러오기
        var heroPrefab = Resources.Load<GameObject>("Prefabs/ch_01_01");
        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(0000);
        hero1.hp = 200;
        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(0000);
        hero2.hp = 200;
        hero2.model = hero2Model;
        heroGO2.transform.position = new Vector3(100);
 
 
 
        GameObject swordGO = new GameObject("Sword");
        sword = swordGO.AddComponent<Sword>();
        sword.damage = 20;
 
        Anim1 = hero1.model.GetComponent<Animation>();
        Anim2 = hero2.model.GetComponent<Animation>();
    }
 
    // 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);
                this.hero2.GetHit(sword.damage);
 
            }
            if (this.elapsedTime1 >= 0.733f)
            {
                this.isAttackAnimationPlay1 = false;
                this.Anim1.Play("attack_sword_02");
                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);
                this.hero2.GetHit(sword.damage);
 
            }
            if (this.elapsedTime1 >= 0.8f)
            {
                this.isAttackAnimationPlay2 = false;
                this.Anim1.Play("attack_sword_03");
                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);
                this.hero2.GetHit(sword.damage);
 
            }
            if (this.elapsedTime1 >= 0.8f)
            {
                this.isAttackAnimationPlay3 = false;
                this.Anim1.Play("idle@loop");
                this.elapsedTime1 = 0;
 
                this.isAttacked = false;
            }
        }
        this.elapsedTime2 += Time.deltaTime;
        if (isAttacked == true && isHero2Alive == true)
        {
            this.Anim2.Play("damage");
        }
        if(hero2.hp <=0 && isHero2Alive == true)
        {
            this.Anim2.Play("die");
            isHero2Alive = false;
        }
        if (this.elapsedTime2 >= 0.8f && isHero2Alive == true)
        {
            this.elapsedTime2 = 0;
            this.Anim2.Play("idle@loop");
        }
        if(hero2.hp >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
 

 

: