'unity 이동 및 공격'에 해당되는 글 1건

  1. 2019.11.13 11.13 unity 유닛 동적생성, 유닛 이동 및 사정거리 내 진입시 공격하기

11.13 unity 유닛 동적생성, 유닛 이동 및 사정거리 내 진입시 공격하기

C#/실습 2019. 11. 13. 17:28

예제:

Hero Go 버튼을 누르기 전

누른 직후

사정 거리 내 도착 후

코드:

 

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
170
171
172
173
174
175
176
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
 
public enum eCharacter
{
    none,
    ch_01_01,
    ch_02_01,
    StoneMonster
}
public class App : MonoBehaviour
{
    public eCharacter eCharacterNames;
    public Button btnCreateStage;
    public Button btnCreateHero;
    public Button btnCreateMonster;
    public Button btnHeroGO;
    private Dictionary<string, GameObject> dicPrefabs;
    private Stage stage;
    private Hero hero;
    private Monster monster;
    private bool isHeroGO = false;
    private bool isAttack = false;
    // Start is called before the first frame update
    void Start()
    {
        int count = 0;
        this.Init();
        this.btnCreateStage.onClick.AddListener(() =>
        {
            this.CreateStage();
        });
 
        this.btnCreateHero.onClick.AddListener(() =>
        {
            if(count ==0)
            {
                this.CreateHero();
                count++;
            }
            else
            {
                Debug.Log("영웅이 이미 생성되었습니다.");
            }
            
        });
        this.btnCreateMonster.onClick.AddListener(() =>
        {
            this.CreateMonster();
        });
        this.btnHeroGO.onClick.AddListener(() =>
        {
            isHeroGO = true;
            
        });
 
    }
    public GameObject FindChild(Transform parent, string childName)
    {
        GameObject childGO = null;
        foreach (Transform child in parent)
        {
            if (child.name == childName)
            {
                childGO = child.gameObject;
                return childGO;
            }
            else
            {
                childGO = FindChild(child.transform, childName);
                if (childGO != null)
                {
                    return childGO;
                }
                continue;
            }
        }
        return childGO;
    }
    public void Init()
    {
        dicPrefabs = new Dictionary<string, GameObject>();
        var prefab1 = Resources.Load<GameObject>("Prefabs/ch_01_01");
        var prefab2 = Resources.Load<GameObject>("Prefabs/ch_02_01");
        var prefabStage = Resources.Load<GameObject>("Prefabs/Stage");
        var prefabMonster = Resources.Load<GameObject>("Prefabs/StoneMonster");
 
 
        dicPrefabs.Add("ch_01_01", prefab1);
        dicPrefabs.Add("ch_02_01", prefab2);
        dicPrefabs.Add("Stage", prefabStage);
        dicPrefabs.Add("StoneMonster", prefabMonster);
        
    }
    public void CreateMonster()
    {
        if (eCharacterNames == eCharacter.none)
        {
            Debug.Log("케릭터 이름을 선택해주세요.");
            return;
        }
 
        GameObject monsterGO = new GameObject("Monster");
        Monster monster = monsterGO.AddComponent<Monster>();
        var prefab = dicPrefabs[eCharacterNames.ToString()];
        var model = Instantiate(prefab);
 
        monster.SetModel(model);
        monsterGO.transform.position = FindChild(stage.transform, "MonsterSpawnP").transform.position;
        monsterGO.transform.LookAt(FindChild(stage.transform, "HeroSpawnP").transform);
 
        this.monster = monster;
    }
    public void CreateStage()
    {
        GameObject stageGO = new GameObject("Stage");
        Stage stage = stageGO.AddComponent<Stage>();
        var prefabs = dicPrefabs["Stage"];
        var model = Instantiate(prefabs);
        model.transform.SetParent(stageGO.transform);
        model.transform.localPosition = Vector3.zero;
        stage.model = model;
        this.stage = stage;
 
    }
    public void CreateHero()
    {
        if(eCharacterNames == eCharacter.none)
        {
            Debug.Log("케릭터 이름을 선택해주세요.");
            return;
        }
 
        GameObject heroGO = new GameObject("Hero");
        Hero hero = heroGO.AddComponent<Hero>();
        var prefab = dicPrefabs[eCharacterNames.ToString()];
        var model = Instantiate(prefab);        
 
        hero.SetModel(model);
        heroGO.transform.position = FindChild(stage.transform, "HeroSpawnP").transform.position;
        heroGO.transform.LookAt(FindChild(stage.transform, "MonsterSpawnP").transform);
 
        hero.attack_range = 1f;
 
        this.hero = hero;
 
    }
 
    // Update is called once per frame
    void Update()
    {
        
        if(isHeroGO == true)
        {
            this.hero.model.GetComponent<Animation>().Play("run@loop");
            this.hero.transform.position = Vector3.Lerp(this.hero.transform.position, this.monster.transform.position, 0.05f);
            this.hero.transform.LookAt(this.monster.transform);
 
            if ((Vector3.Distance(this.hero.transform.position, this.monster.transform.position) <= this.hero.attack_range))
            {
                isHeroGO = false;
                isAttack = true;
            }
        }
        if(isAttack)
        {
            this.hero.model.GetComponent<Animation>().Play("attack_sword_01");
 
        }
 
    }
}
 
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 Stage : MonoBehaviour
{
    public GameObject model;
    // 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
 
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Hero : MonoBehaviour
{
    public GameObject model;
    public float attack_range;
    // Start is called before the first frame update
    void Start()
    {
        
    }
    public void SetModel(GameObject model)
    {
        if (this.model != null)
        {
            Destroy(this.model);
        }
        this.model = model;
        this.model.transform.SetParent(this.gameObject.transform, false);
        this.model.transform.localPosition = Vector3.zero;
    }
 
 
 
    // Update is called once per frame
    void Update()
    {
        
    }
}
 
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Monster : MonoBehaviour
{
    public GameObject model;
 
    // Start is called before the first frame update
    void Start()
    {
        
    }
    public void SetModel(GameObject model)
    {
        if (this.model != null)
        {
            Destroy(this.model);
        }
        this.model = model;
        this.model.transform.SetParent(this.gameObject.transform, false);
        this.model.transform.localPosition = Vector3.zero;
    }
 
    // Update is called once per frame
    void Update()
    {
        
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

: