質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.31%
Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

805閲覧

シューティングゲーム自機に弾を連射させたい

suu1111

総合スコア11

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2022/12/10 13:59

前提

上方向に進むスクリプトをアタッチした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 使用

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

ソースをこうしたらできました

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerShip : MonoBehaviour
{
public AudioClip shotSE;
public GameObject BulletPrefab;
public GameObject FiarPoint;
public GameObject explosion;
private float interval = 0.2f;
private float timer = 0.0f; 

AudioSource audioSource; GameController gameController; // Start is called before the first frame update void Start() { audioSource = GetComponent<AudioSource>(); gameController = GameObject.Find("GameController").GetComponent<GameController>(); } // Update is called once per frame void Update() { Move(); if (Input.GetKey(KeyCode.Z) && timer <= 0.0f)  { Instantiate(BulletPrefab, FiarPoint.transform.position, transform.rotation); timer = interval; } if (timer > 0.0f) { timer -= Time.deltaTime; } } void Move() { float x = Input.GetAxisRaw("Horizontal"); float y = Input.GetAxisRaw("Vertical"); Vector3 nextPoint = transform.position + new Vector3(x, y, 0) * Time.deltaTime * 4; nextPoint = new Vector3( Mathf.Clamp(nextPoint.x,-2.9f,2.9f), Mathf.Clamp(nextPoint.y, -2f, 2f), 0 ); transform.position = nextPoint; } void OnTriggerEnter2D(Collider2D collision) { if (collision.CompareTag("EnemyBullet") == true) { Instantiate(explosion, transform.position, transform.rotation); Destroy(gameObject); Destroy(collision.gameObject); gameController.GameOver(); } }

}

投稿2022/12/11 13:55

suu1111

総合スコア11

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.31%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問