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

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

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

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

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

Q&A

解決済

1回答

598閲覧

【Unity】ゲーム開始前のカウントダウンの実装方法が分からない

kei0105

総合スコア8

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

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

0グッド

0クリップ

投稿2024/08/29 00:37

編集2024/09/10 00:53

実現したいこと

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

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

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

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

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

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

fiveHundred

2024/08/29 10:04

以下を参考にしてみてください。 https://teratail.com/help/question-tips 現状では「どこまで理解しているのか」「どこが分からないのか」が分からないため、代わりに作ることぐらいしか出来ず、それだとあなたの実力になりません(ついでにめんどくさい)。
fiveHundred

2024/08/29 11:03

そもそも、「キャラクターが前進せず停止している状態」は出来ていますか?
guest

回答1

0

自己解決

記事を参考に「TimeManager」スクリプトを作成し、ゲームに適用できる形に落とし込むことができました。

CS

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5using InfiniteRunner; 6 7public class TimeManager : MonoBehaviour 8{ 9 public bool isActive = false; 10 11 [Header("必要なコンポーネントを登録")] 12 [SerializeField] Text countdownText = null; 13 14 [Header("カウントダウンの設定")] 15 [SerializeField] bool countdownEnabled = false; 16 [SerializeField] float countdownWaitTime = 1.0f; 17 [SerializeField] AudioClip[] countdownSe = null; 18 [SerializeField] string goText = "GO!!"; 19 20 InfiniteRunner.GameManager gameManager; 21 SoundManager soundManager; 22 PlayerMovement player; 23 Animator animator; 24 25 void Start() 26 { 27 if (countdownSe == null || countdownSe.Length == 0) 28 { 29 Debug.LogError("Countdown sound effects are not set properly."); 30 } 31 32 gameManager = gameManager = FindObjectOfType<InfiniteRunner.GameManager>(); 33 soundManager = FindObjectOfType<SoundManager>(); 34 player = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMovement>(); 35 36 animator = GameObject.Find("Kiki-v2/Player").GetComponent<Animator>(); 37 38 if (gameManager == null || soundManager == null || player == null) 39 { 40 Debug.LogError("Failed to initialize one or more components."); 41 } 42 if (player == null) 43 { 44 Debug.LogError("Failed to initialize player components."); 45 } 46 if (animator == null) 47 { 48 Debug.LogError("Animator component is missing from the Player GameObject."); 49 } 50 51 StartCoroutine("Countdown"); 52 } 53 54 // ステージスタート時のカウントダウン 55 IEnumerator Countdown() 56 { 57 if (!countdownEnabled || countdownSe.Length == 0) 58 { 59 yield break; 60 } 61 62 isActive = false; 63 64 // プレイヤーを動けなくする(=入力を受け付けないようにする) 65 player.InputEnabled = false; 66 67 // プレイヤーのアニメーションを「Idle」に変更 68 if (animator != null) 69 { 70 animator.Play("Idle"); 71 } 72 73 // カウントダウン開始前の待ち時間 74 yield return new WaitForSeconds(countdownWaitTime); 75 76 countdownText.enabled = true; 77 78 WaitForSeconds waitOneSec = new WaitForSeconds(1); 79 80 // カウントダウンの音声ファイルの数だけカウント 81 for (int i = countdownSe.Length; i >= 1; i--) 82 { 83 if (i == 1) 84 { 85 countdownText.text = goText; 86 87 // 「GO」と同時にRunアニメーションを再生し、プレイヤーを動けるようにする 88 animator.Play("Run"); 89 player.InputEnabled = true; 90 91 // 「GO」が表示されるタイミングでカウントダウン終了 92 isActive = true; 93 } 94 else 95 { 96 countdownText.text = (i - 1).ToString(); 97 } 98 99 if (countdownSe[i - 1] != null) 100 { 101 soundManager.PlaySe(countdownSe[i - 1]); 102 } 103 else 104 { 105 Debug.LogWarning("Countdown sound effect is missing for index: " + (i - 1)); 106 } 107 108 yield return waitOneSec; 109 } 110 111 yield return waitOneSec; 112 113 countdownText.enabled = false; 114 } 115 116} 117

投稿2024/09/10 00:57

kei0105

総合スコア8

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.31%

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

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

質問する

関連した質問