前提・実現したいこと
unityで銃の残弾数がない状態で取得したキーを押すと効果音が鳴ってしまい、困っています。
Eキーでミサイルを撃つのですが、残弾数が0の時に発射キーを押すと、効果音だけが鳴り弾が発射されないといった状況です。
弾の設定としては、弾数数をセットし、弾が無くなると弾、発射アニメーションを実行しないといった感じです。
アドバイスお願いします。
発生している問題・エラーメッセージ
エラーメッセージ 特になし
該当のソースコード
複数音を鳴らすスクリプト using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlaySound : MonoBehaviour { private AudioSource sound01; private AudioSource sound02; void Start() { //AudioSourceコンポーネントを取得し、変数に格納 AudioSource[] audioSources = GetComponents<AudioSource>(); sound01 = audioSources[0]; sound02 = audioSources[1]; } void Update() { //指定のキーが押されたら音声ファイル再生 if (Input.GetKeyDown(KeyCode.Space)) { sound01.PlayOneShot(sound01.clip); } if (Input.GetKeyDown(KeyCode.E)) { sound02.PlayOneShot(sound02.clip); } } }
プレイヤーが撃つミサイルのスクリプト using UnityEngine; using System.Collections; using UnityEngine.UI; public class PlayerMissile : MonoBehaviour { public LayerMask groundLayer; public GameObject Missile; public int shotCount; public Text shellLabel; private Rigidbody2D rigidbody2D; private Animator anim; private bool isGrounded; void Start() { anim = GetComponent<Animator>(); rigidbody2D = GetComponent<Rigidbody2D>(); shellLabel.text = "Missile:" + shotCount; } void Update() { if (Input.GetKeyDown("e")) { if (shotCount < 1) return; // ★追加 shotCountの数値を1ずつ減らす。 shotCount -= 1; // ★追加 shellLabel.text = "Missile:" + shotCount; anim.SetTrigger("Shot"); Instantiate(Missile, transform.position + new Vector3(0.4f, 1.2f, 0f), transform.rotation); } } public void AddShell(int amount) { shotCount += amount; shellLabel.text = "Missile:" + shotCount; } }
試したこと
ネットで効果音の事について調べ、勉強した。
補足情報(FW/ツールのバージョンなど)
unity
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/03 10:14
2018/12/03 10:17
2018/12/03 10:22