teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

情報の追加

2020/05/05 16:56

投稿

unity_user_a
unity_user_a

スコア23

title CHANGED
File without changes
body CHANGED
@@ -188,4 +188,177 @@
188
188
  ![イメージ説明](3af272e3f589e55b1281e00cb367ee46.jpeg)
189
189
  ![イメージ説明](997c60f198fb39eb8aa9d1e6fb19ce94.jpeg)
190
190
 
191
- Unityバージョン:Unity 2019.2.15f1 (64-bit)
191
+ Unityバージョン:Unity 2019.2.15f1 (64-bit)
192
+
193
+ <追記>
194
+ ↓Debug.Logを入れた弾のスクリプト
195
+ ```ここに言語を入力
196
+ using System.Collections;
197
+ using System.Collections.Generic;
198
+ using UnityEngine;
199
+
200
+ public class EnemyAttack : MonoBehaviour
201
+ {
202
+ [Header("スピード")] public float speed = 3.0f;
203
+ [Header("最大移動距離")] public float maxDistance = 100.0f;
204
+
205
+ private Rigidbody2D rb;
206
+ private Vector3 defaultPos;
207
+
208
+
209
+ // Start is called before the first frame update
210
+ void Start()
211
+ {
212
+ rb = GetComponent<Rigidbody2D>();
213
+ if (rb == null)
214
+ {
215
+ Debug.Log("設定が足りません");
216
+ Destroy(this.gameObject);
217
+ }
218
+ defaultPos = transform.position;
219
+ Debug.Log(defaultPos);
220
+
221
+ }
222
+
223
+ // Update is called once per frame
224
+ void FixedUpdate()
225
+ {
226
+ float d = Vector3.Distance(transform.position, defaultPos);
227
+ Debug.Log(transform.position);
228
+
229
+
230
+ //最大移動距離を超えている
231
+ if (d > maxDistance)
232
+ {
233
+ Debug.Log("超えました");
234
+ Destroy(this.gameObject);
235
+ }
236
+ else
237
+ {
238
+ rb.MovePosition(transform.position + Vector3.left * Time.deltaTime * speed);
239
+ }
240
+ }
241
+
242
+ private void OnTriggerEnter2D(Collider2D collision)
243
+ {
244
+ Debug.Log("ぶつかりました");
245
+ Destroy(this.gameObject);
246
+ }
247
+ }
248
+ ```
249
+ ↓移動しない足場の敵で確認 (親オブジェクトである敵の座標:(10.9,-2.5,0))
250
+ ![イメージ説明](24a73c68cb2d76637d390329f7757f3e.jpeg)
251
+ ![イメージ説明](1fe64e02b8d00077623d793325779b46.jpeg)
252
+ 生成されてすぐにぶつかり消滅、
253
+ ![イメージ説明](ef74aab9003248847adda8fcf751ea4a.jpeg)
254
+ ![イメージ説明](29d8dd2b9a85ee43a4a6f4f752604bc7.jpeg)
255
+ 2発目は、y座標の数値が-0.1違う状態で生成され、移動距離の限界まで移動している様です。
256
+
257
+ ↓移動する足場の敵で確認 (親オブジェクトである敵の座標:(22.9,-2.5,0))
258
+ ![イメージ説明](79c1a65ab2a848966e1336a74b190d06.jpeg)
259
+ ![イメージ説明](e5033f30ad1ba94cb9209545714446b5.jpeg)
260
+ 同じく、生成されてすぐにぶつかり消滅、
261
+ ![イメージ説明](9b8e290ccaaa90e4faecb98d01c084c7.jpeg)
262
+ ![イメージ説明](cd91c4e8c52353872706fe0b1581410c.jpeg)
263
+ その後、移動する足場の関係で上下に移動している敵から生成され、
264
+ ![イメージ説明](1da41f0a836c3ed1774723fd7db1b369.jpeg)
265
+ ![イメージ説明](c585acf9a5d9a4acd7eb0b18dd8a82b1.jpeg)
266
+ ![イメージ説明](973142ab6b8e31473dfea0bb023b0855.jpeg)
267
+ しばらくの間、y座標の数値だけ0.1ずつ変化し続け、
268
+ 再びぶつかり判定で消滅していました。
269
+
270
+ ↓移動する足場のスクリプト
271
+ ```ここに言語を入力
272
+ using System.Collections;
273
+ using System.Collections.Generic;
274
+ using UnityEngine;
275
+
276
+ public class MoveObject : MonoBehaviour
277
+ {
278
+ [Header("移動経路")] public GameObject[] movePoint;
279
+ [Header("速さ")] public float speed = 1.0f;
280
+
281
+
282
+ private Rigidbody2D rb;
283
+ private int nowPoint = 0;
284
+ private bool returnPoint = false;
285
+ private Vector2 oldPos = Vector2.zero;
286
+ private Vector2 myVelocity = Vector2.zero;
287
+
288
+ private void Start()
289
+ {
290
+ rb = GetComponent<Rigidbody2D>();
291
+ if (movePoint != null && movePoint.Length > 0 && rb != null)
292
+ {
293
+ rb.position = movePoint[0].transform.position;
294
+ oldPos = rb.position;
295
+ }
296
+ }
297
+
298
+ public Vector2 GetVelocity()
299
+ {
300
+ return myVelocity;
301
+ }
302
+
303
+ private void FixedUpdate()
304
+ {
305
+ if (movePoint != null && movePoint.Length > 1 && rb != null)
306
+ {
307
+ //通常進行
308
+ if (!returnPoint)
309
+ {
310
+ int nextPoint = nowPoint + 1;
311
+
312
+ //目標ポイントとの誤差がわずかになるまで移動
313
+ if (Vector2.Distance(transform.position, movePoint[nextPoint].transform.position) > 0.1f)
314
+ {
315
+ //現在地から次のポイントへのベクトルを作成
316
+ Vector2 toVector = Vector2.MoveTowards(transform.position, movePoint[nextPoint].transform.position, speed * Time.deltaTime);
317
+
318
+ //次のポイントへ移動
319
+ rb.MovePosition(toVector);
320
+ }
321
+ //次のポイントを1つ進める
322
+ else
323
+ {
324
+ rb.MovePosition(movePoint[nextPoint].transform.position);
325
+ ++nowPoint;
326
+ //現在地が配列の最後だった場合
327
+ if (nowPoint + 1 >= movePoint.Length)
328
+ {
329
+ returnPoint = true;
330
+ }
331
+ }
332
+ }
333
+ //折返し進行
334
+ else
335
+ {
336
+ int nextPoint = nowPoint - 1;
337
+
338
+ //目標ポイントとの誤差がわずかになるまで移動
339
+ if (Vector2.Distance(transform.position, movePoint[nextPoint].transform.position) > 0.1f)
340
+ {
341
+ //現在地から次のポイントへのベクトルを作成
342
+ Vector2 toVector = Vector2.MoveTowards(transform.position, movePoint[nextPoint].transform.position, speed * Time.deltaTime);
343
+
344
+ //次のポイントへ移動
345
+ rb.MovePosition(toVector);
346
+ }
347
+ //次のポイントを1つ戻す
348
+ else
349
+ {
350
+ rb.MovePosition(movePoint[nextPoint].transform.position);
351
+ --nowPoint;
352
+ //現在地が配列の最初だった場合
353
+ if (nowPoint <= 0)
354
+ {
355
+ returnPoint = false;
356
+ }
357
+ }
358
+ }
359
+ myVelocity = (rb.position - oldPos) / Time.deltaTime;
360
+ oldPos = rb.position;
361
+ }
362
+ }
363
+ }
364
+ ```

1

スクリプトを「画像で添付」から、「コードの挿入」に変更

2020/05/05 16:56

投稿

unity_user_a
unity_user_a

スコア23

title CHANGED
File without changes
body CHANGED
@@ -13,14 +13,171 @@
13
13
  可能であれば、1つ目の問題と併せてご回答・アドバイス頂きたいです。
14
14
 
15
15
  ↓敵のスクリプト
16
+ ```ここに言語を入力
17
+ using System.Collections;
16
- ![イメージ説明](89c73658ec11c04e7468a207214c60f4.jpeg)
18
+ using System.Collections.Generic;
17
- ![イメージ説明](dedc6da86ad340144cf1103c77019eb8.jpeg)
19
+ using UnityEngine;
18
- ![イメージ説明](0b48df3a2587331156afabf3f08aba41.jpeg)
19
20
 
21
+ public class Enemy_Zako9 : MonoBehaviour
22
+ {
23
+ [Header("攻撃オブジェクト")] public GameObject attackObj;
24
+ [Header("攻撃間隔")] public float interval;
25
+
26
+ [Header("重力")] public float gravity;
27
+ [Header("やられた時に鳴らすSE")] public AudioClip deadSE;
28
+
29
+
30
+ private Rigidbody2D rb = null;
31
+ private SpriteRenderer sr = null;
32
+
33
+ private ObjectCollision oc = null;
34
+ private BoxCollider2D col = null;
35
+ private bool isDead = false;
36
+ private float deadTimer = 0.0f;
37
+
38
+ private Animator anim;
39
+ private float timer;
40
+
41
+ // Start is called before the first frame update
42
+ void Start()
43
+ {
44
+ rb = GetComponent<Rigidbody2D>();
45
+ sr = GetComponent<SpriteRenderer>();
46
+ oc = GetComponent<ObjectCollision>();
47
+ col = GetComponent<BoxCollider2D>();
48
+
49
+ anim = GetComponent<Animator>();
50
+ if (anim == null || attackObj == null)
51
+ {
52
+ Debug.Log("設定が足りません");
53
+ Destroy(this.gameObject);
54
+ }
55
+ else
56
+ {
57
+ attackObj.SetActive(false);
58
+ }
59
+ }
60
+
61
+ // Update is called once per frame
62
+ void Update()
63
+ {
64
+ AnimatorStateInfo currentState = anim.GetCurrentAnimatorStateInfo(0);
65
+
66
+ //通常の状態
67
+ if (currentState.IsName("zako-9_idle"))
68
+ {
69
+ if (timer > interval)
70
+ {
71
+ anim.SetTrigger("attack");
72
+ timer = 0.0f;
73
+ }
74
+ else
75
+ {
76
+ timer += Time.deltaTime;
77
+ }
78
+ }
79
+ }
80
+
81
+ void FixedUpdate()
82
+ {
83
+ if (!oc.playerStepOn)
84
+ {
85
+ if (sr.isVisible )
86
+ {
87
+
88
+
89
+
90
+
91
+ }
92
+ }
93
+ else
94
+ {
95
+ if (!isDead)
96
+ {
97
+ anim.Play("zako-9_dead");
98
+ rb.velocity = new Vector2(0, -gravity);
99
+ isDead = true;
100
+ col.enabled = false;
101
+ if (GManager.instance != null)
102
+ {
103
+ GManager.instance.PlaySE(deadSE);
104
+
105
+ }
106
+ }
107
+ else
108
+ {
109
+
110
+ if (deadTimer > 1.0f)
111
+ {
112
+ Destroy(this.gameObject);
113
+ }
114
+ else
115
+ {
116
+ deadTimer += Time.deltaTime;
117
+ }
118
+ }
119
+ }
120
+ }
121
+
122
+ public void Attack()
123
+ {
124
+ GameObject g = Instantiate(attackObj);
125
+ g.transform.SetParent(transform);
126
+ g.transform.position = attackObj.transform.position;
127
+ g.SetActive(true);
128
+ }
129
+ }
130
+ ```
131
+
20
132
  ↓敵が発射する弾のスクリプト
133
+ ```ここに言語を入力
134
+ using System.Collections;
21
- ![イメージ説明](66e80df72cbca228ea9704a809d4917a.jpeg)
135
+ using System.Collections.Generic;
22
- ![イメージ説明](dc18216a1cc82e0c4d0f5dfbcd087261.jpeg)
136
+ using UnityEngine;
23
137
 
138
+ public class EnemyAttack : MonoBehaviour
139
+ {
140
+ [Header("スピード")] public float speed = 3.0f;
141
+ [Header("最大移動距離")] public float maxDistance = 100.0f;
142
+
143
+ private Rigidbody2D rb;
144
+ private Vector3 defaultPos;
145
+
146
+ // Start is called before the first frame update
147
+ void Start()
148
+ {
149
+ rb = GetComponent<Rigidbody2D>();
150
+ if (rb == null)
151
+ {
152
+ Debug.Log("設定が足りません");
153
+ Destroy(this.gameObject);
154
+ }
155
+ defaultPos = transform.position;
156
+ }
157
+
158
+ // Update is called once per frame
159
+ void FixedUpdate()
160
+ {
161
+ float d = Vector3.Distance(transform.position, defaultPos);
162
+
163
+ //最大移動距離を超えている
164
+ if (d > maxDistance)
165
+ {
166
+ Destroy(this.gameObject);
167
+ }
168
+ else
169
+ {
170
+ rb.MovePosition(transform.position + Vector3.left * Time.deltaTime * speed);
171
+ }
172
+ }
173
+
174
+ private void OnTriggerEnter2D(Collider2D collision)
175
+ {
176
+ Destroy(this.gameObject);
177
+ }
178
+ }
179
+ ```
180
+
24
181
  ↓移動する足場にいる敵の情報
25
182
  ![イメージ説明](dac177a808beca13ccbff54c740735ab.jpeg)
26
183
  ![イメージ説明](efa746ac521aa6dee7492a4a341ac27b.jpeg)