質問編集履歴
2
情報の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -379,3 +379,349 @@
|
|
379
379
|
|
380
380
|
|
381
381
|
Unityバージョン:Unity 2019.2.15f1 (64-bit)
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
<追記>
|
386
|
+
|
387
|
+
↓Debug.Logを入れた弾のスクリプト
|
388
|
+
|
389
|
+
```ここに言語を入力
|
390
|
+
|
391
|
+
using System.Collections;
|
392
|
+
|
393
|
+
using System.Collections.Generic;
|
394
|
+
|
395
|
+
using UnityEngine;
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
public class EnemyAttack : MonoBehaviour
|
400
|
+
|
401
|
+
{
|
402
|
+
|
403
|
+
[Header("スピード")] public float speed = 3.0f;
|
404
|
+
|
405
|
+
[Header("最大移動距離")] public float maxDistance = 100.0f;
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
private Rigidbody2D rb;
|
410
|
+
|
411
|
+
private Vector3 defaultPos;
|
412
|
+
|
413
|
+
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
// Start is called before the first frame update
|
418
|
+
|
419
|
+
void Start()
|
420
|
+
|
421
|
+
{
|
422
|
+
|
423
|
+
rb = GetComponent<Rigidbody2D>();
|
424
|
+
|
425
|
+
if (rb == null)
|
426
|
+
|
427
|
+
{
|
428
|
+
|
429
|
+
Debug.Log("設定が足りません");
|
430
|
+
|
431
|
+
Destroy(this.gameObject);
|
432
|
+
|
433
|
+
}
|
434
|
+
|
435
|
+
defaultPos = transform.position;
|
436
|
+
|
437
|
+
Debug.Log(defaultPos);
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
}
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
// Update is called once per frame
|
446
|
+
|
447
|
+
void FixedUpdate()
|
448
|
+
|
449
|
+
{
|
450
|
+
|
451
|
+
float d = Vector3.Distance(transform.position, defaultPos);
|
452
|
+
|
453
|
+
Debug.Log(transform.position);
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
|
458
|
+
|
459
|
+
//最大移動距離を超えている
|
460
|
+
|
461
|
+
if (d > maxDistance)
|
462
|
+
|
463
|
+
{
|
464
|
+
|
465
|
+
Debug.Log("超えました");
|
466
|
+
|
467
|
+
Destroy(this.gameObject);
|
468
|
+
|
469
|
+
}
|
470
|
+
|
471
|
+
else
|
472
|
+
|
473
|
+
{
|
474
|
+
|
475
|
+
rb.MovePosition(transform.position + Vector3.left * Time.deltaTime * speed);
|
476
|
+
|
477
|
+
}
|
478
|
+
|
479
|
+
}
|
480
|
+
|
481
|
+
|
482
|
+
|
483
|
+
private void OnTriggerEnter2D(Collider2D collision)
|
484
|
+
|
485
|
+
{
|
486
|
+
|
487
|
+
Debug.Log("ぶつかりました");
|
488
|
+
|
489
|
+
Destroy(this.gameObject);
|
490
|
+
|
491
|
+
}
|
492
|
+
|
493
|
+
}
|
494
|
+
|
495
|
+
```
|
496
|
+
|
497
|
+
↓移動しない足場の敵で確認 (親オブジェクトである敵の座標:(10.9,-2.5,0))
|
498
|
+
|
499
|
+
![イメージ説明](24a73c68cb2d76637d390329f7757f3e.jpeg)
|
500
|
+
|
501
|
+
![イメージ説明](1fe64e02b8d00077623d793325779b46.jpeg)
|
502
|
+
|
503
|
+
生成されてすぐにぶつかり消滅、
|
504
|
+
|
505
|
+
![イメージ説明](ef74aab9003248847adda8fcf751ea4a.jpeg)
|
506
|
+
|
507
|
+
![イメージ説明](29d8dd2b9a85ee43a4a6f4f752604bc7.jpeg)
|
508
|
+
|
509
|
+
2発目は、y座標の数値が-0.1違う状態で生成され、移動距離の限界まで移動している様です。
|
510
|
+
|
511
|
+
|
512
|
+
|
513
|
+
↓移動する足場の敵で確認 (親オブジェクトである敵の座標:(22.9,-2.5,0))
|
514
|
+
|
515
|
+
![イメージ説明](79c1a65ab2a848966e1336a74b190d06.jpeg)
|
516
|
+
|
517
|
+
![イメージ説明](e5033f30ad1ba94cb9209545714446b5.jpeg)
|
518
|
+
|
519
|
+
同じく、生成されてすぐにぶつかり消滅、
|
520
|
+
|
521
|
+
![イメージ説明](9b8e290ccaaa90e4faecb98d01c084c7.jpeg)
|
522
|
+
|
523
|
+
![イメージ説明](cd91c4e8c52353872706fe0b1581410c.jpeg)
|
524
|
+
|
525
|
+
その後、移動する足場の関係で上下に移動している敵から生成され、
|
526
|
+
|
527
|
+
![イメージ説明](1da41f0a836c3ed1774723fd7db1b369.jpeg)
|
528
|
+
|
529
|
+
![イメージ説明](c585acf9a5d9a4acd7eb0b18dd8a82b1.jpeg)
|
530
|
+
|
531
|
+
![イメージ説明](973142ab6b8e31473dfea0bb023b0855.jpeg)
|
532
|
+
|
533
|
+
しばらくの間、y座標の数値だけ0.1ずつ変化し続け、
|
534
|
+
|
535
|
+
再びぶつかり判定で消滅していました。
|
536
|
+
|
537
|
+
|
538
|
+
|
539
|
+
↓移動する足場のスクリプト
|
540
|
+
|
541
|
+
```ここに言語を入力
|
542
|
+
|
543
|
+
using System.Collections;
|
544
|
+
|
545
|
+
using System.Collections.Generic;
|
546
|
+
|
547
|
+
using UnityEngine;
|
548
|
+
|
549
|
+
|
550
|
+
|
551
|
+
public class MoveObject : MonoBehaviour
|
552
|
+
|
553
|
+
{
|
554
|
+
|
555
|
+
[Header("移動経路")] public GameObject[] movePoint;
|
556
|
+
|
557
|
+
[Header("速さ")] public float speed = 1.0f;
|
558
|
+
|
559
|
+
|
560
|
+
|
561
|
+
|
562
|
+
|
563
|
+
private Rigidbody2D rb;
|
564
|
+
|
565
|
+
private int nowPoint = 0;
|
566
|
+
|
567
|
+
private bool returnPoint = false;
|
568
|
+
|
569
|
+
private Vector2 oldPos = Vector2.zero;
|
570
|
+
|
571
|
+
private Vector2 myVelocity = Vector2.zero;
|
572
|
+
|
573
|
+
|
574
|
+
|
575
|
+
private void Start()
|
576
|
+
|
577
|
+
{
|
578
|
+
|
579
|
+
rb = GetComponent<Rigidbody2D>();
|
580
|
+
|
581
|
+
if (movePoint != null && movePoint.Length > 0 && rb != null)
|
582
|
+
|
583
|
+
{
|
584
|
+
|
585
|
+
rb.position = movePoint[0].transform.position;
|
586
|
+
|
587
|
+
oldPos = rb.position;
|
588
|
+
|
589
|
+
}
|
590
|
+
|
591
|
+
}
|
592
|
+
|
593
|
+
|
594
|
+
|
595
|
+
public Vector2 GetVelocity()
|
596
|
+
|
597
|
+
{
|
598
|
+
|
599
|
+
return myVelocity;
|
600
|
+
|
601
|
+
}
|
602
|
+
|
603
|
+
|
604
|
+
|
605
|
+
private void FixedUpdate()
|
606
|
+
|
607
|
+
{
|
608
|
+
|
609
|
+
if (movePoint != null && movePoint.Length > 1 && rb != null)
|
610
|
+
|
611
|
+
{
|
612
|
+
|
613
|
+
//通常進行
|
614
|
+
|
615
|
+
if (!returnPoint)
|
616
|
+
|
617
|
+
{
|
618
|
+
|
619
|
+
int nextPoint = nowPoint + 1;
|
620
|
+
|
621
|
+
|
622
|
+
|
623
|
+
//目標ポイントとの誤差がわずかになるまで移動
|
624
|
+
|
625
|
+
if (Vector2.Distance(transform.position, movePoint[nextPoint].transform.position) > 0.1f)
|
626
|
+
|
627
|
+
{
|
628
|
+
|
629
|
+
//現在地から次のポイントへのベクトルを作成
|
630
|
+
|
631
|
+
Vector2 toVector = Vector2.MoveTowards(transform.position, movePoint[nextPoint].transform.position, speed * Time.deltaTime);
|
632
|
+
|
633
|
+
|
634
|
+
|
635
|
+
//次のポイントへ移動
|
636
|
+
|
637
|
+
rb.MovePosition(toVector);
|
638
|
+
|
639
|
+
}
|
640
|
+
|
641
|
+
//次のポイントを1つ進める
|
642
|
+
|
643
|
+
else
|
644
|
+
|
645
|
+
{
|
646
|
+
|
647
|
+
rb.MovePosition(movePoint[nextPoint].transform.position);
|
648
|
+
|
649
|
+
++nowPoint;
|
650
|
+
|
651
|
+
//現在地が配列の最後だった場合
|
652
|
+
|
653
|
+
if (nowPoint + 1 >= movePoint.Length)
|
654
|
+
|
655
|
+
{
|
656
|
+
|
657
|
+
returnPoint = true;
|
658
|
+
|
659
|
+
}
|
660
|
+
|
661
|
+
}
|
662
|
+
|
663
|
+
}
|
664
|
+
|
665
|
+
//折返し進行
|
666
|
+
|
667
|
+
else
|
668
|
+
|
669
|
+
{
|
670
|
+
|
671
|
+
int nextPoint = nowPoint - 1;
|
672
|
+
|
673
|
+
|
674
|
+
|
675
|
+
//目標ポイントとの誤差がわずかになるまで移動
|
676
|
+
|
677
|
+
if (Vector2.Distance(transform.position, movePoint[nextPoint].transform.position) > 0.1f)
|
678
|
+
|
679
|
+
{
|
680
|
+
|
681
|
+
//現在地から次のポイントへのベクトルを作成
|
682
|
+
|
683
|
+
Vector2 toVector = Vector2.MoveTowards(transform.position, movePoint[nextPoint].transform.position, speed * Time.deltaTime);
|
684
|
+
|
685
|
+
|
686
|
+
|
687
|
+
//次のポイントへ移動
|
688
|
+
|
689
|
+
rb.MovePosition(toVector);
|
690
|
+
|
691
|
+
}
|
692
|
+
|
693
|
+
//次のポイントを1つ戻す
|
694
|
+
|
695
|
+
else
|
696
|
+
|
697
|
+
{
|
698
|
+
|
699
|
+
rb.MovePosition(movePoint[nextPoint].transform.position);
|
700
|
+
|
701
|
+
--nowPoint;
|
702
|
+
|
703
|
+
//現在地が配列の最初だった場合
|
704
|
+
|
705
|
+
if (nowPoint <= 0)
|
706
|
+
|
707
|
+
{
|
708
|
+
|
709
|
+
returnPoint = false;
|
710
|
+
|
711
|
+
}
|
712
|
+
|
713
|
+
}
|
714
|
+
|
715
|
+
}
|
716
|
+
|
717
|
+
myVelocity = (rb.position - oldPos) / Time.deltaTime;
|
718
|
+
|
719
|
+
oldPos = rb.position;
|
720
|
+
|
721
|
+
}
|
722
|
+
|
723
|
+
}
|
724
|
+
|
725
|
+
}
|
726
|
+
|
727
|
+
```
|
1
スクリプトを「画像で添付」から、「コードの挿入」に変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -28,19 +28,333 @@
|
|
28
28
|
|
29
29
|
↓敵のスクリプト
|
30
30
|
|
31
|
+
```ここに言語を入力
|
32
|
+
|
33
|
+
using System.Collections;
|
34
|
+
|
35
|
+
using System.Collections.Generic;
|
36
|
+
|
37
|
+
using UnityEngine;
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
public class Enemy_Zako9 : MonoBehaviour
|
42
|
+
|
43
|
+
{
|
44
|
+
|
45
|
+
[Header("攻撃オブジェクト")] public GameObject attackObj;
|
46
|
+
|
31
|
-
|
47
|
+
[Header("攻撃間隔")] public float interval;
|
32
|
-
|
33
|
-
|
48
|
+
|
34
|
-
|
49
|
+
|
50
|
+
|
35
|
-
|
51
|
+
[Header("重力")] public float gravity;
|
52
|
+
|
53
|
+
[Header("やられた時に鳴らすSE")] public AudioClip deadSE;
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
private Rigidbody2D rb = null;
|
60
|
+
|
61
|
+
private SpriteRenderer sr = null;
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
private ObjectCollision oc = null;
|
66
|
+
|
67
|
+
private BoxCollider2D col = null;
|
68
|
+
|
69
|
+
private bool isDead = false;
|
70
|
+
|
71
|
+
private float deadTimer = 0.0f;
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
private Animator anim;
|
76
|
+
|
77
|
+
private float timer;
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
// Start is called before the first frame update
|
82
|
+
|
83
|
+
void Start()
|
84
|
+
|
85
|
+
{
|
86
|
+
|
87
|
+
rb = GetComponent<Rigidbody2D>();
|
88
|
+
|
89
|
+
sr = GetComponent<SpriteRenderer>();
|
90
|
+
|
91
|
+
oc = GetComponent<ObjectCollision>();
|
92
|
+
|
93
|
+
col = GetComponent<BoxCollider2D>();
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
anim = GetComponent<Animator>();
|
98
|
+
|
99
|
+
if (anim == null || attackObj == null)
|
100
|
+
|
101
|
+
{
|
102
|
+
|
103
|
+
Debug.Log("設定が足りません");
|
104
|
+
|
105
|
+
Destroy(this.gameObject);
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
else
|
110
|
+
|
111
|
+
{
|
112
|
+
|
113
|
+
attackObj.SetActive(false);
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
// Update is called once per frame
|
122
|
+
|
123
|
+
void Update()
|
124
|
+
|
125
|
+
{
|
126
|
+
|
127
|
+
AnimatorStateInfo currentState = anim.GetCurrentAnimatorStateInfo(0);
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
//通常の状態
|
132
|
+
|
133
|
+
if (currentState.IsName("zako-9_idle"))
|
134
|
+
|
135
|
+
{
|
136
|
+
|
137
|
+
if (timer > interval)
|
138
|
+
|
139
|
+
{
|
140
|
+
|
141
|
+
anim.SetTrigger("attack");
|
142
|
+
|
143
|
+
timer = 0.0f;
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
else
|
148
|
+
|
149
|
+
{
|
150
|
+
|
151
|
+
timer += Time.deltaTime;
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
void FixedUpdate()
|
162
|
+
|
163
|
+
{
|
164
|
+
|
165
|
+
if (!oc.playerStepOn)
|
166
|
+
|
167
|
+
{
|
168
|
+
|
169
|
+
if (sr.isVisible )
|
170
|
+
|
171
|
+
{
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
else
|
186
|
+
|
187
|
+
{
|
188
|
+
|
189
|
+
if (!isDead)
|
190
|
+
|
191
|
+
{
|
192
|
+
|
193
|
+
anim.Play("zako-9_dead");
|
194
|
+
|
195
|
+
rb.velocity = new Vector2(0, -gravity);
|
196
|
+
|
197
|
+
isDead = true;
|
198
|
+
|
199
|
+
col.enabled = false;
|
200
|
+
|
201
|
+
if (GManager.instance != null)
|
202
|
+
|
203
|
+
{
|
204
|
+
|
205
|
+
GManager.instance.PlaySE(deadSE);
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
else
|
214
|
+
|
215
|
+
{
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
if (deadTimer > 1.0f)
|
220
|
+
|
221
|
+
{
|
222
|
+
|
223
|
+
Destroy(this.gameObject);
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
else
|
228
|
+
|
229
|
+
{
|
230
|
+
|
231
|
+
deadTimer += Time.deltaTime;
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
public void Attack()
|
244
|
+
|
245
|
+
{
|
246
|
+
|
247
|
+
GameObject g = Instantiate(attackObj);
|
248
|
+
|
249
|
+
g.transform.SetParent(transform);
|
250
|
+
|
251
|
+
g.transform.position = attackObj.transform.position;
|
252
|
+
|
253
|
+
g.SetActive(true);
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
```
|
36
260
|
|
37
261
|
|
38
262
|
|
39
263
|
↓敵が発射する弾のスクリプト
|
40
264
|
|
265
|
+
```ここに言語を入力
|
266
|
+
|
267
|
+
using System.Collections;
|
268
|
+
|
269
|
+
using System.Collections.Generic;
|
270
|
+
|
271
|
+
using UnityEngine;
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
public class EnemyAttack : MonoBehaviour
|
276
|
+
|
277
|
+
{
|
278
|
+
|
279
|
+
[Header("スピード")] public float speed = 3.0f;
|
280
|
+
|
281
|
+
[Header("最大移動距離")] public float maxDistance = 100.0f;
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
private Rigidbody2D rb;
|
286
|
+
|
287
|
+
private Vector3 defaultPos;
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
// Start is called before the first frame update
|
292
|
+
|
293
|
+
void Start()
|
294
|
+
|
295
|
+
{
|
296
|
+
|
297
|
+
rb = GetComponent<Rigidbody2D>();
|
298
|
+
|
299
|
+
if (rb == null)
|
300
|
+
|
301
|
+
{
|
302
|
+
|
303
|
+
Debug.Log("設定が足りません");
|
304
|
+
|
305
|
+
Destroy(this.gameObject);
|
306
|
+
|
307
|
+
}
|
308
|
+
|
309
|
+
defaultPos = transform.position;
|
310
|
+
|
311
|
+
}
|
312
|
+
|
313
|
+
|
314
|
+
|
41
|
-
|
315
|
+
// Update is called once per frame
|
316
|
+
|
42
|
-
|
317
|
+
void FixedUpdate()
|
318
|
+
|
319
|
+
{
|
320
|
+
|
321
|
+
float d = Vector3.Distance(transform.position, defaultPos);
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
//最大移動距離を超えている
|
326
|
+
|
327
|
+
if (d > maxDistance)
|
328
|
+
|
329
|
+
{
|
330
|
+
|
331
|
+
Destroy(this.gameObject);
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
else
|
336
|
+
|
337
|
+
{
|
338
|
+
|
339
|
+
rb.MovePosition(transform.position + Vector3.left * Time.deltaTime * speed);
|
340
|
+
|
341
|
+
}
|
342
|
+
|
343
|
+
}
|
344
|
+
|
345
|
+
|
346
|
+
|
43
|
-
|
347
|
+
private void OnTriggerEnter2D(Collider2D collision)
|
348
|
+
|
349
|
+
{
|
350
|
+
|
351
|
+
Destroy(this.gameObject);
|
352
|
+
|
353
|
+
}
|
354
|
+
|
355
|
+
}
|
356
|
+
|
357
|
+
```
|
44
358
|
|
45
359
|
|
46
360
|
|