【Unity】弾の種類を切り替える

投稿者: | 2021-01-24

銃のオブジェクトがマガジンのオブジェクトを持つようにして、簡単に弾の種類を切り替えてみました。

まず、マガジンと弾のクラスを作って、それらを継承した赤い弾の入ったマガジンクラスと青い弾の入ったマガジンクラスを作りました。

弾のクラスでは弾を飛ばす力と発射メソッドを定義しています。

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(Rigidbody))]
  6. [RequireComponent(typeof(Collider))]
  7. public class Bullet : MonoBehaviour
  8. {
  9. [SerializeField] float power = 20f;
  10. // Start is called before the first frame update
  11. void Start()
  12. {
  13. Destroy(gameObject, 4f);
  14. }
  15.  
  16. public void Shoot()
  17. {
  18. Rigidbody rb = GetComponent<Rigidbody>();
  19. rb.AddForce(transform.forward * power, ForceMode.Impulse);
  20. }
  21.  
  22. }

これを継承した二種類の弾クラスを作って、シーン上のSphereにアタッチしてプレハブ化しておきます。インスペクタで弾の種類毎に違った値を設定できます。

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class RedBullet : Bullet
  6. {
  7.  
  8. }
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class BlueBullet : Bullet
  6. {
  7.  
  8. }

マガジンクラスは弾のプレハブと弾数のデータを持っていて、射撃メソッドが呼ばれると弾をインスタンス化して、弾のメソッドを呼びます。

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Magazine : MonoBehaviour
  6. {
  7. [SerializeField] Bullet bullet;
  8. [SerializeField] int count = 20;
  9.  
  10. public int GetCount()
  11. {
  12. return count;
  13. }
  14.  
  15. public bool Shoot()
  16. {
  17. if (count > 0)
  18. {
  19. Bullet b = Instantiate(bullet, Player2.GetInstance().transform.position + Player2.GetInstance().transform.forward * 1.5f, Player2.GetInstance().transform.rotation).GetComponent<Bullet>();
  20. b.Shoot();
  21.  
  22. count--;
  23. GameScript3.GetInstance().SetBulletCount(this);
  24.  
  25. return true;
  26. }
  27. else
  28. {
  29. return false;
  30. }
  31. }
  32. }

これを継承したマガジンクラスを二種類作りました。今回はこれらを空のゲームオブジェクトにアタッチしてシーンに配置しました。インスペクタで弾の種類と残弾数を個別に設定できます。

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class RedMagazine : Magazine
  6. {
  7.  
  8. }
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class BlueMagazine : Magazine
  6. {
  7.  
  8. }

銃のクラスを作るためにまずは武器の抽象クラスを作りました。このクラスでは、マウスクリックしたときとクリックを放した時に呼ぶメソッドと寿命を作りました。

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. abstract public class Weapon : MonoBehaviour
  6. {
  7. abstract public void StartAttack();
  8. abstract public void StopAttack();
  9. [SerializeField] protected float lifespan = 1f;
  10.  
  11. public float GetLifespan()
  12. {
  13. return lifespan;
  14. }
  15. }

そして、これを継承した小火器クラスを作りました。小火器クラスではマガジンと発射メソッドを定義します。マガジンは自由に入れ替えられます。とりあえずマウスクリックされると一度発射メソッドを呼んで、放すと何もしないようにしました。

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class SmallArms : Weapon
  6. {
  7. [SerializeField] Magazine magazine;
  8.  
  9. public void SetMagazine(Magazine m)
  10. {
  11. magazine = m;
  12. }
  13.  
  14. public Magazine GetMagazine()
  15. {
  16. return magazine;
  17. }
  18.  
  19. protected void Shoot()
  20. {
  21. if (magazine == null) return;
  22. if (magazine.Shoot())
  23. {
  24. lifespan -= 0.01f;
  25. }
  26. }
  27.  
  28. public override void StartAttack()
  29. {
  30. Shoot();
  31. }
  32.  
  33. public override void StopAttack()
  34. {
  35.  
  36.  
  37. }
  38. }

弾が発射されると武器の寿命を少し減らします。

さらにこれを継承したハンドガンクラスとマシンガンクラスを作りました。これらも空のゲームオブジェクトにアタッチしてシーンに置きました。

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class HandGun1 : SmallArms
  6. {
  7.  
  8. }
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class MachineGun1 : SmallArms
  6. {
  7. WaitForSeconds wait;
  8. [SerializeField] float rate = 0.1f;
  9.  
  10. private void Awake()
  11. {
  12. wait = new WaitForSeconds(rate);
  13. }
  14.  
  15. public override void StartAttack()
  16. {
  17. StartCoroutine("RapidFire");
  18. }
  19.  
  20. public override void StopAttack()
  21. {
  22. StopCoroutine("RapidFire");
  23. }
  24.  
  25. IEnumerator RapidFire()
  26. {
  27. while (true)
  28. {
  29. Shoot();
  30. yield return wait;
  31. }
  32. }
  33. }

マシンガンクラスでは、連射コルーチンの繰り返し処理の中で発射メソッドを呼び、マウスクリックで連射を開始、放すと停止します。ハンドガンには無い連射速度のフィールドもあります。

プレイヤーの頭に付けたスクリプトで武器クラスの入れ替えと攻撃を行います。

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player2 : MonoBehaviour
  6. {
  7. [SerializeField] Weapon weapon;
  8.  
  9. public void SetWeapon(Weapon w)
  10. {
  11. weapon = w;
  12. }
  13.  
  14. public Weapon GetWeapon()
  15. {
  16. return weapon;
  17. }
  18.  
  19. static Player2 instance;
  20. public static Player2 GetInstance()
  21. {
  22. return instance;
  23. }
  24.  
  25. // Start is called before the first frame update
  26. void Start()
  27. {
  28. instance = this;
  29. }
  30.  
  31. // Update is called once per frame
  32. void Update()
  33. {
  34. if(Input.GetMouseButtonDown(0))
  35. {
  36. if (weapon != null) weapon.StartAttack();
  37.  
  38. }else if(Input.GetMouseButtonUp(0))
  39. {
  40. if (weapon != null) weapon.StopAttack();
  41. }
  42. }
  43. }

今回はとりあえずキー入力で武器やマガジンを入れ替えます。

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class GameScript3 : MonoBehaviour
  7. {
  8. [SerializeField] Magazine redMagazine;
  9. [SerializeField] Magazine blueMagazine;
  10. [SerializeField] Weapon handGun1;
  11. [SerializeField] Weapon machineGun1;
  12.  
  13. [SerializeField] Text magazineText;
  14. [SerializeField] Text weaponText;
  15.  
  16. static GameScript3 instance;
  17. public static GameScript3 GetInstance()
  18. {
  19. return instance;
  20. }
  21.  
  22. // Start is called before the first frame update
  23. void Start()
  24. {
  25. instance = this;
  26. magazineText.text = "";
  27. weaponText.text = "";
  28. }
  29.  
  30. // Update is called once per frame
  31. void Update()
  32. {
  33. if(Input.GetKeyDown(KeyCode.Alpha1))
  34. {
  35. SmallArms g = (SmallArms)Player2.GetInstance().GetWeapon();
  36. g.SetMagazine(redMagazine);
  37.  
  38. SetBulletCount(redMagazine);
  39. }
  40. else if (Input.GetKeyDown(KeyCode.Alpha2))
  41. {
  42. SmallArms g = (SmallArms)Player2.GetInstance().GetWeapon();
  43. g.SetMagazine(blueMagazine);
  44.  
  45. SetBulletCount(blueMagazine);
  46.  
  47. }
  48. else if(Input.GetKeyDown(KeyCode.E))
  49. {
  50. Player2.GetInstance().SetWeapon(handGun1);
  51. weaponText.text = handGun1.name;
  52.  
  53. SetBulletCount((SmallArms)handGun1);
  54.  
  55. } else if(Input.GetKeyDown(KeyCode.Q))
  56. {
  57. Player2.GetInstance().SetWeapon(machineGun1);
  58. weaponText.text = machineGun1.name;
  59.  
  60. SetBulletCount((SmallArms)machineGun1);
  61.  
  62. }
  63. }
  64.  
  65. public void SetBulletCount(Magazine magazine)
  66. {
  67. magazineText.text = magazine.name + "\n" + magazine.GetCount().ToString();
  68. }
  69.  
  70. void SetBulletCount(SmallArms smallArms)
  71. {
  72. SmallArms sa = smallArms;
  73. Magazine m = sa.GetMagazine();
  74. if (m == null)
  75. {
  76. magazineText.text = "";
  77. }else
  78. {
  79. SetBulletCount(m);
  80. }
  81. }
  82.  
  83. }

これで武器やマガジンを簡単に入れ替えられるようになりました。武器オブジェクトがマガジンオブジェクトを持っているので、2種類の武器に別のマガジンオブジェクトを付けていれば、武器を切り替えると出る弾の種類も変わります。

コメントを残す

メールアドレスが公開されることはありません。