質問編集履歴
1
改良点の追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -275,3 +275,257 @@
|
|
275
275
|
情報等足りないところや、わかりにくいところ等あれば追記します。
|
276
276
|
|
277
277
|
ぜひ回答よろしくお願いします。
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
以下追記になります。
|
288
|
+
|
289
|
+
kaka1102さんの回答を参考にいろいろ変更してみました。
|
290
|
+
|
291
|
+
プレイヤーはエネミーの弾は管理していなく、プレイヤーとエネミーが共通して持っているスペースシップというスクリプトで弾の撃つ撃たないを管理しています。
|
292
|
+
|
293
|
+
なので、参考サイト、kaka1102さんが仮に作成したBulletGeneratorを組み合わせて、スペースシップに追加してみました。
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
```lang-C#
|
298
|
+
|
299
|
+
using UnityEngine;
|
300
|
+
|
301
|
+
using System.Collections;
|
302
|
+
|
303
|
+
using System.Collections.Generic;
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
[RequireComponent(typeof(Rigidbody2D))]
|
308
|
+
|
309
|
+
public class Spaceship : MonoBehaviour {
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
List<Bullet> list_Bullets = new List<Bullet>();
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
const int MAX_BULLETS = 20;
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
public float speed;
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
public float shotDelay;
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
public GameObject bullet;
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
public bool canShot;
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
public GameObject explosion;
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
void Start()
|
344
|
+
|
345
|
+
{
|
346
|
+
|
347
|
+
Bullet bulleta;
|
348
|
+
|
349
|
+
// 最初に一定数の弾を備蓄しておく
|
350
|
+
|
351
|
+
for (int i = 0; i < MAX_BULLETS; i++)
|
352
|
+
|
353
|
+
{
|
354
|
+
|
355
|
+
// 弾の生成
|
356
|
+
|
357
|
+
bulleta = Instantiate(bullet).GetComponent<Bullet>();
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
bulleta.transform.parent = this.transform;
|
362
|
+
|
363
|
+
// 発射前は非アクティブにしておく
|
364
|
+
|
365
|
+
bulleta.gameObject.SetActive(false);
|
366
|
+
|
367
|
+
// Listに追加
|
368
|
+
|
369
|
+
list_Bullets.Add(bulleta);
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
public void Shot(Transform origin)
|
380
|
+
|
381
|
+
{
|
382
|
+
|
383
|
+
for (int i = 0; i < list_Bullets.Count; i++)
|
384
|
+
|
385
|
+
{
|
386
|
+
|
387
|
+
{
|
388
|
+
|
389
|
+
if (!list_Bullets[i].gameObject.activeSelf)
|
390
|
+
|
391
|
+
{
|
392
|
+
|
393
|
+
list_Bullets[i].transform.position = origin.position;
|
394
|
+
|
395
|
+
list_Bullets[i].transform.rotation = origin.rotation;
|
396
|
+
|
397
|
+
list_Bullets[i].gameObject.SetActive(true);
|
398
|
+
|
399
|
+
break;
|
400
|
+
|
401
|
+
}
|
402
|
+
|
403
|
+
}
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
//Instantiate(bullet, origin.position, origin.rotation);
|
412
|
+
|
413
|
+
}
|
414
|
+
|
415
|
+
}
|
416
|
+
|
417
|
+
public void Move(Vector2 direction)
|
418
|
+
|
419
|
+
{
|
420
|
+
|
421
|
+
GetComponent<Rigidbody2D>().velocity = direction * speed;
|
422
|
+
|
423
|
+
}
|
424
|
+
|
425
|
+
public void Explosion()
|
426
|
+
|
427
|
+
{
|
428
|
+
|
429
|
+
Instantiate(explosion, transform.position, transform.rotation);
|
430
|
+
|
431
|
+
}
|
432
|
+
|
433
|
+
|
434
|
+
|
435
|
+
}
|
436
|
+
|
437
|
+
```
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
![イメージ説明](3b8c49875d8a35603b48ba56efc12963.png)
|
442
|
+
|
443
|
+
すると、最初の一発は弾を発射するんですが、次からはこのように止まってしまいました。
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
この状態では、エラーは出ませんが、画像にあるDestroyAreaという画面外のオブジェクトに当たりエネミーが破壊され、次のエネミーが出てきたところで
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
UnassignedReferenceException: The variable bullet of Spaceship has not been assigned.
|
452
|
+
|
453
|
+
You probably need to assign the bullet variable of the Spaceship script in the inspector.
|
454
|
+
|
455
|
+
UnityEngine.Object.Instantiate[GameObject] (UnityEngine.GameObject original) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:189)
|
456
|
+
|
457
|
+
|
458
|
+
|
459
|
+
という同じエラーが3つ出てきます。
|
460
|
+
|
461
|
+
このエラーをダブルクリックした先はスペースシップスクリプトの
|
462
|
+
|
463
|
+
bulleta = Instantiate(bullet).GetComponent<Bullet>();
|
464
|
+
|
465
|
+
になります。
|
466
|
+
|
467
|
+
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
画像中でプレイヤーか弾が出ていませんが、プレイヤーの弾はDestroyAreaにあたると、非表示になった状態で、上へ永遠に飛んで行っていました。
|
472
|
+
|
473
|
+
|
474
|
+
|
475
|
+
DestroyAreaのスクリプトはこちらです。
|
476
|
+
|
477
|
+
|
478
|
+
|
479
|
+
```lang-C#
|
480
|
+
|
481
|
+
using UnityEngine;
|
482
|
+
|
483
|
+
using System.Collections;
|
484
|
+
|
485
|
+
|
486
|
+
|
487
|
+
public class DestroyArea : MonoBehaviour {
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
void OnTriggerEnter2D(Collider2D c)
|
492
|
+
|
493
|
+
{
|
494
|
+
|
495
|
+
if (c.tag == "Bullet(Player)")
|
496
|
+
|
497
|
+
{
|
498
|
+
|
499
|
+
c.gameObject.SetActive(false);
|
500
|
+
|
501
|
+
}
|
502
|
+
|
503
|
+
else if (c.tag == "Bullet(Enemy)") {
|
504
|
+
|
505
|
+
c.gameObject.SetActive(false);
|
506
|
+
|
507
|
+
}
|
508
|
+
|
509
|
+
else
|
510
|
+
|
511
|
+
{
|
512
|
+
|
513
|
+
Destroy(c.gameObject);
|
514
|
+
|
515
|
+
}
|
516
|
+
|
517
|
+
}
|
518
|
+
|
519
|
+
}
|
520
|
+
|
521
|
+
|
522
|
+
|
523
|
+
```
|
524
|
+
|
525
|
+
|
526
|
+
|
527
|
+
以上kaka1102さんの回答を参考に改良した点になります。
|
528
|
+
|
529
|
+
|
530
|
+
|
531
|
+
よろしくお願いします。
|