実現したいこと
Unity でゲーム開始前のカウントダウンの実装を行いたい。
カウントダウン後にゲームキャラクターが動きだし、ゲームが開始されるようにしたい。
発生している問題・分からないこと
以下、記事に沿ってランゲームを作成しました。
https://feynman.co.jp/unityforest/unity-introduction/3d-run-game/
しかし、完成したものは突然スタートするゲームになっています。
下記事を参考にゲーム開始前のカウントダウン実装したいのですが、弊ゲームにてどのようなコードやUI操作を行えばよいか不明です。
https://unity.moon-bear.com/unitychan-parkour-2020/timer-and-countdown/
該当のソースコード
TimeManager.cs
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class TimeManager : MonoBehaviour 7{ 8 9 public bool isActive = false; 10 11 [Header("必要なコンポーネントを登録")] 12 [SerializeField] 13 Text timerText = null; 14 [SerializeField] 15 Text countdownText = null; 16 17 [Header("時間の設定")] 18 [SerializeField] 19 int minutes = 0; 20 [SerializeField] 21 float seconds = 0; 22 23 [Header("カウントダウンの設定")] 24 [SerializeField] 25 bool countdownEnabled = false; 26 [SerializeField] 27 float countdownWaitTime = 1.5f; 28 [SerializeField] 29 AudioClip[] countdownSe = null; 30 [SerializeField] 31 string goText = "GO!!"; 32 33 float currentTime; 34 float oldSeconds; 35 float totalTime; 36 37 GameManager gameManager; 38 SoundManager soundManager; 39 KurokumaCharacterController player; 40 41 // 現在の時間を取得できるプロパティ(※今回は未使用) 42 public float CurrentTime 43 { 44 set 45 { 46 currentTime = value; 47 currentTime = Mathf.Max(currentTime, 0); 48 49 if (currentTime > totalTime) 50 { 51 totalTime = currentTime; 52 } 53 } 54 get 55 { 56 return currentTime; 57 } 58 } 59 60 public float TotalTime 61 { 62 get 63 { 64 return totalTime; 65 } 66 } 67 68 public float RemainingTime 69 { 70 get 71 { 72 return totalTime - currentTime; 73 } 74 } 75 76 void Start() 77 { 78 gameManager = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameManager>(); 79 soundManager = GameObject.FindGameObjectWithTag("GameController").GetComponent<SoundManager>(); 80 player = GameObject.FindGameObjectWithTag("Player").GetComponent<KurokumaCharacterController>(); 81 82 InitTime(); 83 StartCoroutine("Countdown"); 84 } 85 86 void Update() 87 { 88 Timer(); 89 } 90 91 public void InitTime() 92 { 93 oldSeconds = 0; 94 totalTime = minutes * 60 + seconds; 95 currentTime = totalTime; 96 97 UpdateTimeText(); 98 } 99 100 // カウントダウンタイマー 101 void Timer() 102 { 103 if (!isActive || currentTime <= 0f) 104 { 105 return; 106 } 107 108 currentTime = minutes * 60 + seconds; 109 currentTime -= Time.deltaTime; 110 111 minutes = (int)currentTime / 60; 112 seconds = currentTime - minutes * 60; 113 114 if ((int)seconds != (int)oldSeconds) 115 { 116 UpdateTimeText(); 117 } 118 119 oldSeconds = seconds; 120 121 if (currentTime <= 0f) 122 { 123 isActive = false; 124 125 gameManager.GameOver(); 126 } 127 } 128 129 void UpdateTimeText() 130 { 131 timerText.text = minutes.ToString("D2") + ":" + ((int)seconds).ToString("D2"); 132 } 133 134 // ステージスタート時のカウントダウン 135 IEnumerator Countdown() 136 { 137 if (!countdownEnabled || countdownSe.Length == 0) 138 { 139 yield break; 140 } 141 142 isActive = false; 143 144 // プレイヤーを動けなくする(=入力を受け付けないようにする) 145 player.InputEnabled = false; 146 147 // カウントダウン開始前の待ち時間 148 yield return new WaitForSeconds(countdownWaitTime); 149 150 countdownText.enabled = true; 151 152 WaitForSeconds waitOneSec = new WaitForSeconds(1); 153 154 // カウントダウンの音声ファイルの数だけカウント 155 for (int i = countdownSe.Length; i >= 1; i--) 156 { 157 if (i == 1) 158 { 159 countdownText.text = goText; 160 } 161 else 162 { 163 countdownText.text = (i - 1).ToString(); 164 } 165 166 if (countdownSe[i - 1] != null) 167 { 168 soundManager.PlaySe(countdownSe[i - 1]); 169 } 170 171 yield return waitOneSec; 172 } 173 174 isActive = true; 175 176 // プレイヤーを動けるようにする 177 player.InputEnabled = true; 178 179 yield return waitOneSec; 180 181 countdownText.enabled = false; 182 } 183 184}
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
補足
Unityバージョン:2021.3.23.f1
回答1件
あなたの回答
tips
プレビュー