前提
上方向に進むスクリプトをアタッチしたBulletPrefabをZキーを押したときに生成するスクリプトを書きました
実現したいこと
Zキーを押しているときに弾を一定間隔で連射し、Zキーを押すのをやめると連射がストップするようにしたいです。
試したこと
検索で出たループ文のソースを書き込んでみた
発生している問題・エラーメッセージ
うまく球を発射することができなくなった
該当のソースコード
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class PlayerShip : MonoBehaviour 6{ 7 public AudioClip shotSE; 8 public GameObject BulletPrefab; 9 public GameObject FiarPoint; 10 public GameObject explosion; 11 12 13 AudioSource audioSource; 14 GameController gameController; 15 16 // Start is called before the first frame update 17 void Start() 18 { 19 audioSource = GetComponent<AudioSource>(); 20 gameController = GameObject.Find("GameController").GetComponent<GameController>(); 21 22 } 23 24 // Update is called once per frame 25 void Update() 26 { 27 Move(); 28 Shot(); 29 30 } 31 32 void Move() 33 { 34 float x = Input.GetAxisRaw("Horizontal"); 35 float y = Input.GetAxisRaw("Vertical"); 36 37 Vector3 nextPoint = transform.position + new Vector3(x, y, 0) * Time.deltaTime * 4; 38 39 nextPoint = new Vector3( 40 Mathf.Clamp(nextPoint.x,-2.9f,2.9f), 41 Mathf.Clamp(nextPoint.y, -2f, 2f), 42 0 43 ); 44 45 transform.position = nextPoint; 46 } 47 void Shot() 48 { 49 if(Input.GetKeyDown(KeyCode.Z)) 50 { 51 52 audioSource.PlayOneShot(shotSE); 53 Instantiate(BulletPrefab, FiarPoint.transform.position, transform.rotation); 54 } 55 } 56 57 void OnTriggerEnter2D(Collider2D collision) 58 { 59 if (collision.CompareTag("EnemyBullet") == true) 60 { 61 Instantiate(explosion, transform.position, transform.rotation); 62 Destroy(gameObject); 63 Destroy(collision.gameObject); 64 gameController.GameOver(); 65 } 66 } 67}
補足情報(FW/ツールのバージョンなど)
unity 2021.3.14f1 visualstudio2022 使用

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。