UnityにてKキーを押したら弾丸を発射する仕様にしようとコードを書きました。ゲームを実行してKキーを押すと、ヒエラルキービューにはちゃんと表示されているが、ゲーム画面に弾丸が表示されません。一時停止を押すと弾丸は表示されるのですが再び実行するとまた消えます。コードの間違いがあれば教えてほしいです。
using
1using System.Collections.Generic; 2using UnityEngine; 3 4public class shoot_controller : MonoBehaviour 5{ 6 Rigidbody2D sk; 7 float xidou = 0.0f; 8 public float speed = 3.0f; 9 public float jump = 3.0f; 10 bool goJump = false; 11 bool onGround = false; 12 int jumpCount = 0; 13 public LayerMask groundLayer; 14 public Transform shootPoint; 15 public GameObject dangan; 16 // Start is called before the first frame update 17 void Start() 18 { 19 sk = this.GetComponent<Rigidbody2D>(); 20 21 } 22 23 // Update is called once per frame 24 void Update() 25 { 26 Attack(); 27 xidou = Input.GetAxisRaw("Horizontal"); 28 29 //地面と接触 30 onGround = Physics2D.Linecast(transform.position, transform.position - (transform.up * 0.1f), groundLayer); 31 32 33 if (jumpCount < 2 && Input.GetButtonDown("Jump")) 34 { 35 Vector2 jumpPw = new Vector2(0, jump); 36 sk.AddForce(jumpPw, ForceMode2D.Impulse); 37 jumpCount++; 38 } 39 40 //地面についたらカウントリセット 41 if (onGround) 42 { 43 jumpCount = 0; 44 } 45 46 47 } 48 void FixedUpdate() 49 { 50 sk.velocity = new Vector2(speed * xidou, 0); 51 52 } 53 54 //Kキーで発射 55 public void Attack() 56 { 57 if (Input.GetKeyDown(KeyCode.K)) 58 { 59 Instantiate(dangan, shootPoint.position, Quaternion.identity); 60 61 } 62 63 } 64} 65 66 67 68コード
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/02/27 15:26