###前提・実現したいこと
チャージ攻撃を実装したのですが、効果音がうまく機能しません。
ボタンを押した瞬間にチャージ開始の効果音が発生して、チャージカウンターの値が21~22の間で終了するのでそこから、チャージ音を鳴らしてループさせたいです。
###発生している問題・エラーメッセージ
ループが再生が一回しか行われない。
音がとにかく連続で再生される。
チャージ攻撃を連射するとコルーチンの処理が溜まって一気に音が再生される
###該当のソースコード
C# ここにご自身が実行したソースコードを書いてください using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class PlayerShootArrow : MonoBehaviour { public GameObject shot; public GameObject Noshot; public GameObject muzzle; public GameObject muzzleFlash; public GameObject tame; public GameObject parentObject; GameObject Obj; GameObject Player; //チャージカウント public bool charge = false; public GameObject refObj; public int chargeCount = 0; public int chargeCountMax = 100; int displaychargeCount; public Text chargeCountText; public Image ArrowPowerGauge; public GameObject muzzleGuide; public GameObject ArrowReticle; [Header("Color")]//チャージゲージの色を指定 public Color ArrowPowerL1; public Color ArrowPowerL2; public Color ArrowPowerL3; public Color ArrowPowerL4; //音関係 AudioSource aud; public AudioClip ChargeStart; public AudioClip ChargeStay; public AudioClip ChargeEnd; // Use this for initialization void Start() { charge = false; chargeCount = displaychargeCount; chargeCount = 0; chargeCountMax = 100; aud = gameObject.GetComponent<AudioSource>(); Player = GameObject.Find("sira"); muzzleGuide.gameObject.SetActive(false); ArrowReticle.gameObject.SetActive(false); StartCoroutine(ChargeSound()); } // Update is called once per frame void Update() { if (Input.GetButton("Kamae")) {//構えボタンを押している if (Input.GetButtonDown("AtackX")) {//AtackXボタンを押した瞬間 Quaternion rote = Quaternion.Euler(0.0f, 90.0f, 0.0f); Obj = (GameObject)Instantiate(Noshot, muzzle.transform.position, Player.transform.rotation); Obj.transform.parent = Player.transform; charge = true; //矢のチャージゲージのオンオフ PlayerArrowChargeCountOnOff a = refObj.GetComponent<PlayerArrowChargeCountOnOff>(); if (a != null) { a.ChargeOn(); } //矢のチャージグラを表示する tame = Instantiate(muzzleFlash, muzzle.transform.position, Player.transform.rotation) as GameObject; tame.transform.parent = Player.transform; //効果音 aud.PlayOneShot(this.ChargeStart); /* Quaternion rote = Quaternion.Euler(90.0f, 0.0f, 0.0f); Obj = (GameObject)Instantiate(shot, muzzle.transform.position, rote); Obj = (GameObject)Instantiate(shot, this.transform.position, Quaternion.identity); */ /* マズルフラッシュを表示する Instantiate(muzzleFlash, muzzle.transform.position, transform.rotation); audioSource.PlayOneShot(audioSource.clip); */ } else if (Input.GetButtonUp("AtackX")) {//AtackXボタンを離した瞬間 Instantiate(shot, muzzle.transform.position, Player.transform.rotation); chargeCount = 0; charge = false; //効果音 aud.PlayOneShot(this.ChargeEnd); } //チャージアタック関連 chargeCountText.text = string.Format("{0:000}%", displaychargeCount); if (displaychargeCount != chargeCount) displaychargeCount = (int)Mathf.Lerp(displaychargeCount, chargeCount, 1f); if (Input.GetButton("AtackX")) {//AtackXボタンを押している間チャージカウント増加 chargeCount++; if (chargeCount >= 100) {//チャージカウント100以上なら100固定 chargeCount = 100; } } else {//AtackXボタンを離したらチャージカウントリセット PlayerArrowChargeCountOnOff b = refObj.GetComponent<PlayerArrowChargeCountOnOff>(); if (b != null) { b.ChargeOff(); } //矢のチャージグラを削除 Destroy(tame); } } else { chargeCount = 0; } } IEnumerator ChargeSound() { while (chargeCount <= 21) { yield return new WaitForSeconds(0.1f); } while (chargeCount >= 21) { Debug.Log("bbbbbbbb"); //溜め効果音の再生 this.aud.PlayOneShot(this.ChargeStay); yield return new WaitForSeconds(0.5f); } yield return null; } }
###試したこと
StartCoroutineをvoid Startに記述
ループが再生が一回しか行われない。
StartCoroutineをif (Input.GetButton("AtackX"))内にif (chargeCount >= 22)を作成し記述
音がとにかく連続で再生される。
StartCoroutineをif (Input.GetButtonDown("AtackX"))内に記述
チャージ攻撃を連射(カウンターが22以下)するとコルーチンの処理が溜まって一気に音が再生される
###補足情報(言語/FW/ツール等のバージョンなど)
【version等】
言語C#
Unity 5.6.1f1 Personal
チャージ攻撃のループ音は0.5fで1再生になっています。
0.5f待ってもう一度ループというのがコルーチン以外で自分がやり方を知らないだけで、他にやり方があるなら他の方法でも良いです。(ですが、できればコルーチン等で解決できると理想的です。)
上記のプログラムは溜め攻撃を一度しかしないなら理想的な動きになります。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/07 01:14
2017/07/24 04:28 編集
2017/07/24 04:51