Unityでカーリングゲームを作る #7 勝敗を表示して次のシーンまでカウントダウンする

投稿者: | 2019-12-28



勝敗をテキスト表示して、次のシーンを読み込むまでカウントダウンしてみます。

8個目のストーンが投げられた後、そのストーンの速度が0になるとスコアを計算するためのコライダーとスクリプトが付いたオブジェクトがアクティブになります。

  1. if (StoneScript.num == 8 && StoneScript.gameState == 3)//Input.GetKeyDown(KeyCode.Space)
  2. {
  3. StoneScript.gameState = 4;
  4. house.SetActive(!house.activeSelf);
  5.  
  6. }

アクティブになるとコライダーの中にあるストーンを調べて、中心からの距離が近い順にソートし、スコアを計算します。それと同時にカウントダウンも始まります。

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System.Linq;
  6. using UnityEngine.SceneManagement;
  7.  
  8.  
  9. public class HouseScript : MonoBehaviour
  10. {
  11. bool secondLap;
  12. List<int> stoneNumbers;
  13. Dictionary<GameObject, float> stonesInHouse;
  14. public Transform center;
  15. bool winner;
  16. int score;
  17.  
  18.  
  19. public Text scoreText;
  20. public Text restartText;
  21.  
  22. // Start is called before the first frame update
  23. void Start()
  24. {
  25.  
  26. secondLap = false;
  27. score = 0;
  28.  
  29. stoneNumbers = new List<int>();
  30. stonesInHouse = new Dictionary<GameObject, float>();
  31.  
  32. scoreText.text = "Draw";
  33.  
  34. StartCoroutine("CountDown");
  35. }
  36.  
  37. // Update is called once per frame
  38. void Update()
  39. {
  40. }
  41.  
  42. private void OnTriggerStay(Collider other)
  43. {
  44. // ストーンの番号が一巡していないとき
  45. if (!secondLap && other.CompareTag("Stone"))
  46. {
  47. // ストーンの番号がリストにあるとき
  48. if (stoneNumbers.Count != 0 && stoneNumbers.Contains(other.gameObject.GetComponent<StoneScript>().stoneNumber))
  49. {
  50. secondLap = true;
  51. Score();
  52. }
  53. else
  54. {// ストーンの番号がリストに無いとき
  55. Debug.Log(other.gameObject.GetComponent<StoneScript>().stoneNumber);
  56. stoneNumbers.Add(other.gameObject.GetComponent<StoneScript>().stoneNumber); // 番号をリストに追加する
  57. stonesInHouse.Add(other.gameObject, Vector3.Distance(other.gameObject.transform.position, center.position)); // オブジェクトと中心までの距離をディクショナリーに追加する
  58. }
  59.  
  60. }
  61.  
  62. }
  63.  
  64. void Score()
  65. {
  66. var stoneDic = stonesInHouse.OrderBy((x) => x.Value); // ディクショナリーの値をソートする
  67. int n = 0;
  68. foreach (KeyValuePair<GameObject, float> stone in stoneDic)
  69. {
  70. StoneScript sc_stone = stone.Key.GetComponent<StoneScript>();
  71.  
  72. //Debug.Log(stone.Key.transform.position.ToString() + " " + stone.Value.ToString() + " " + stone.Key.GetComponent<StoneScript>().stoneNumber);
  73.  
  74.  
  75. if (n == 0)
  76. {
  77. winner = sc_stone.player; // 中心に一番近いストーンの手番を勝者にする
  78. score++; // スコアに1を足す
  79. }
  80. else if (sc_stone.player == winner)
  81. {
  82.  
  83. score++; // 続くストーンの手番が勝者と同じであればスコアに1追加
  84. }
  85. else if (sc_stone.player != winner)
  86. {
  87. break; // 勝者と同じでなければスコアの計算を終える
  88. }
  89.  
  90. n++;
  91.  
  92. }
  93.  
  94. Debug.Log("winner:" + winner + " score:" + score);
  95.  
  96. if (winner)
  97. {
  98. scoreText.text = "YOU WIN\nscore:" + score;
  99. }
  100. else {
  101.  
  102. scoreText.text = "YOU LOSE\nscore:" + -score;
  103. }
  104. }
  105.  
  106. private IEnumerator CountDown()
  107. {
  108. yield return new WaitForSeconds(0.4f);
  109. int second = 5;
  110.  
  111. while (second > 0)
  112. {
  113.  
  114. restartText.text = second+"秒後に再読み込みします";
  115.  
  116. yield return new WaitForSeconds(1.0f);
  117. second--;
  118. }
  119.  
  120. SceneManager.LoadScene("curling");
  121. }
  122.  
  123. }

OnTriggerStayでストーンがあるか調べます。ストーンが無いときはDrawと表示したいですが、ストーンが無いとOnTriggerStayの処理が実行されません。
なので、スタート時にDrawと表示して、ストーンがある場合は、勝敗の表示に差し替えるようにしました。

カウントダウンにはコルーチンを使います。

  1. private IEnumerator CountDown()
  2. {
  3. yield return new WaitForSeconds(0.4f);
  4. int second = 5;
  5.  
  6. while (second > 0)
  7. {
  8.  
  9. restartText.text = second+"秒後に再読み込みします";
  10.  
  11. yield return new WaitForSeconds(1.0f);
  12. second--;
  13. }
  14.  
  15. SceneManager.LoadScene("curling");
  16. }

while文の繰り返し処理を1秒おきにやります。それが終わるとシーンを再読み込みします。

コメントを残す

メールアドレスが公開されることはありません。