質問編集履歴

4

補足情報

2020/02/10 23:04

投稿

leeee1019
leeee1019

スコア9

test CHANGED
File without changes
test CHANGED
@@ -291,3 +291,5 @@
291
291
 
292
292
 
293
293
  クラロワとゆうのは、フィールドにキャラを出して戦うカードゲームです
294
+
295
+ https://clashroyale.com/ja/

3

補足情報

2020/02/10 23:04

投稿

leeee1019
leeee1019

スコア9

test CHANGED
File without changes
test CHANGED
@@ -290,4 +290,4 @@
290
290
 
291
291
 
292
292
 
293
- ここより詳細な情報記載してください。
293
+ クラロワとゆうのは、フィールドキャラして戦うカードゲームです

2

言語名

2020/02/10 23:02

投稿

leeee1019
leeee1019

スコア9

test CHANGED
File without changes
test CHANGED
@@ -270,7 +270,7 @@
270
270
 
271
271
  }
272
272
 
273
- ```ここに言語名を入力
273
+ C♯ここに言語名を入力
274
274
 
275
275
  ソースコード
276
276
 

1

ソースコード

2020/02/04 10:34

投稿

leeee1019
leeee1019

スコア9

test CHANGED
File without changes
test CHANGED
@@ -18,22 +18,266 @@
18
18
 
19
19
  エラーメッセージ
20
20
 
21
+ 特にないですが、キャラは自動生成です。
22
+
23
+
24
+
25
+ ### 該当のソースコード
26
+
27
+ using UnityEngine;
28
+
29
+ using UnityEngine.AI;
30
+
31
+ [RequireComponent(typeof(NavMeshAgent))]
32
+
33
+ public class Character : MonoBehaviour
34
+
35
+ {
36
+
37
+ private GameObject target;
38
+
39
+ private NavMeshAgent agent;
40
+
41
+ [SerializeField] private GameObject[] enemies;
42
+
43
+ [SerializeField] private string attackTag;
44
+
45
+ [SerializeField] private string attackTag1;//Inspectorで設定
46
+
47
+ private float stopDistance;
48
+
49
+ private float attackTimer;
50
+
51
+ private bool isAttack;
52
+
53
+ //ここから
54
+
55
+ public int distance;
56
+
57
+ public int hp;
58
+
59
+ public int power;
60
+
61
+ public int attackTime;
62
+
63
+ //ここまでInspectorで設定
64
+
65
+ private enum targetType
66
+
67
+ {
68
+
69
+ normal,
70
+
71
+ castle,
72
+
73
+ }
74
+
75
+ private targetType currentTargetType;
76
+
77
+
78
+
79
+ private void Awake()
80
+
81
+ {
82
+
83
+ agent = GetComponent<NavMeshAgent>();
84
+
85
+ }
86
+
87
+ private void Start()
88
+
89
+ {
90
+
91
+ target = GameObject.Find(attackTag+attackTag1 + "Castle");
92
+
93
+ isAttack = false;
94
+
95
+ }
96
+
97
+ private void Update()
98
+
99
+ {
100
+
101
+ if (target == null)
102
+
103
+ {
104
+
105
+ target = GameObject.Find(attackTag+ attackTag1 + "Castle");
106
+
107
+ }
108
+
109
+
110
+
111
+ SetStopDistance();
112
+
113
+ FintTarget();
114
+
115
+ agent.SetDestination(target.transform.position);
116
+
117
+
118
+
119
+ if (Vector3.Distance(transform.position, target.transform.position) <= stopDistance)
120
+
121
+ {
122
+
123
+ isAttack = true;
124
+
125
+ agent.speed = 0;
126
+
127
+ }
128
+
129
+
130
+
131
+ if (isAttack)
132
+
133
+ {
134
+
135
+ CheckNearTarget();
136
+
137
+ SetStopDistance(); //攻撃中にtargetが変わった時のためにここでも記述
138
+
139
+ Attack();
140
+
141
+ }
142
+
143
+ }
144
+
145
+ //targetが近くにいるのかどうか判定する
146
+
147
+ private void CheckNearTarget()
148
+
149
+ {
150
+
151
+ if (Vector3.Distance(transform.position, target.transform.position) > stopDistance)
152
+
153
+ {
154
+
155
+ isAttack = false;
156
+
157
+ agent.speed = 1;
158
+
159
+ }
160
+
161
+ }
162
+
163
+ private void SetStopDistance()
164
+
165
+ {
166
+
167
+ if (target.gameObject.name.Contains("Castle"))
168
+
169
+ {
170
+
171
+ currentTargetType = targetType.castle;
172
+
173
+ stopDistance = distance;
174
+
175
+ }
176
+
177
+ else
178
+
179
+ {
180
+
181
+ currentTargetType = targetType.normal;
182
+
183
+ stopDistance = distance;
184
+
185
+ }
186
+
187
+ }
188
+
189
+ private void FintTarget()
190
+
191
+ {
192
+
193
+ enemies = GameObject.FindGameObjectsWithTag(attackTag+ attackTag1);
194
+
195
+
196
+
197
+ float closestDistance = Vector3.Distance(transform.position, target.transform.position);
198
+
199
+
200
+
201
+ foreach (GameObject enemy in enemies)
202
+
203
+ {
204
+
205
+ if (Vector3.Distance(transform.position, enemy.transform.position) < closestDistance)
206
+
207
+ {
208
+
209
+ target = enemy;
210
+
211
+ }
212
+
213
+ }
214
+
215
+ }
216
+
217
+ private void Attack()
218
+
219
+ {
220
+
221
+ attackTimer += Time.deltaTime;
222
+
223
+ switch (currentTargetType)
224
+
225
+ {
226
+
227
+ case (targetType.normal):
228
+
229
+ if (attackTimer > 1)
230
+
231
+ {
232
+
233
+ target.GetComponent<Character>().hp -= power;
234
+
235
+ attackTimer = 0f;
236
+
237
+ }
238
+
239
+ if (target.GetComponent<Character>().hp <= 0)
240
+
241
+ {
242
+
243
+ isAttack = false;
244
+
245
+ Destroy(target.gameObject);
246
+
247
+ agent.speed = 1;
248
+
249
+ }
250
+
251
+ break;
252
+
253
+ case (targetType.castle):
254
+
255
+ if (attackTimer > attackTime)
256
+
257
+ {
258
+
259
+ target.GetComponent<Castle>().hp -= power;
260
+
261
+ attackTimer = 0f;
262
+
263
+ }
264
+
265
+ break;
266
+
267
+ }
268
+
269
+ }
270
+
271
+ }
272
+
273
+ ```ここに言語名を入力
274
+
275
+ ソースコード
276
+
21
277
  ```
22
278
 
23
279
 
24
280
 
25
- ### 該当のソースコード
26
-
27
-
28
-
29
- ```ここに言語名を入力
30
-
31
- ソースコード
32
-
33
- ```
34
-
35
-
36
-
37
281
  ### 試したこと
38
282
 
39
283