勝敗をテキスト表示して、次のシーンを読み込むまでカウントダウンしてみます。
8個目のストーンが投げられた後、そのストーンの速度が0になるとスコアを計算するためのコライダーとスクリプトが付いたオブジェクトがアクティブになります。
- if (StoneScript.num == 8 && StoneScript.gameState == 3)//Input.GetKeyDown(KeyCode.Space)
- {
- StoneScript.gameState = 4;
- house.SetActive(!house.activeSelf);
-
- }
アクティブになるとコライダーの中にあるストーンを調べて、中心からの距離が近い順にソートし、スコアを計算します。それと同時にカウントダウンも始まります。
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using System.Linq;
- using UnityEngine.SceneManagement;
-
-
- public class HouseScript : MonoBehaviour
- {
- bool secondLap;
- List<int> stoneNumbers;
- Dictionary<GameObject, float> stonesInHouse;
- public Transform center;
- bool winner;
- int score;
-
-
- public Text scoreText;
- public Text restartText;
-
- // Start is called before the first frame update
- void Start()
- {
-
- secondLap = false;
- score = 0;
-
- stoneNumbers = new List<int>();
- stonesInHouse = new Dictionary<GameObject, float>();
-
- scoreText.text = "Draw";
-
- StartCoroutine("CountDown");
- }
-
- // Update is called once per frame
- void Update()
- {
-
- }
-
- private void OnTriggerStay(Collider other)
- {
-
- // ストーンの番号が一巡していないとき
- if (!secondLap && other.CompareTag("Stone"))
- {
- // ストーンの番号がリストにあるとき
- if (stoneNumbers.Count != 0 && stoneNumbers.Contains(other.gameObject.GetComponent<StoneScript>().stoneNumber))
- {
- secondLap = true;
- Score();
- }
- else
- {// ストーンの番号がリストに無いとき
- Debug.Log(other.gameObject.GetComponent<StoneScript>().stoneNumber);
- stoneNumbers.Add(other.gameObject.GetComponent<StoneScript>().stoneNumber); // 番号をリストに追加する
- stonesInHouse.Add(other.gameObject, Vector3.Distance(other.gameObject.transform.position, center.position)); // オブジェクトと中心までの距離をディクショナリーに追加する
- }
-
- }
-
- }
-
- void Score()
- {
-
- var stoneDic = stonesInHouse.OrderBy((x) => x.Value); // ディクショナリーの値をソートする
- int n = 0;
- foreach (KeyValuePair<GameObject, float> stone in stoneDic)
- {
- StoneScript sc_stone = stone.Key.GetComponent<StoneScript>();
-
- //Debug.Log(stone.Key.transform.position.ToString() + " " + stone.Value.ToString() + " " + stone.Key.GetComponent<StoneScript>().stoneNumber);
-
-
- if (n == 0)
- {
- winner = sc_stone.player; // 中心に一番近いストーンの手番を勝者にする
- score++; // スコアに1を足す
- }
- else if (sc_stone.player == winner)
- {
-
- score++; // 続くストーンの手番が勝者と同じであればスコアに1追加
- }
- else if (sc_stone.player != winner)
- {
- break; // 勝者と同じでなければスコアの計算を終える
- }
-
- n++;
-
- }
-
- Debug.Log("winner:" + winner + " score:" + score);
-
- if (winner)
- {
- scoreText.text = "YOU WIN\nscore:" + score;
- }
- else {
-
- scoreText.text = "YOU LOSE\nscore:" + -score;
- }
- }
-
- private IEnumerator CountDown()
- {
- yield return new WaitForSeconds(0.4f);
- int second = 5;
-
- while (second > 0)
- {
-
- restartText.text = second+"秒後に再読み込みします";
-
- yield return new WaitForSeconds(1.0f);
- second--;
- }
-
- SceneManager.LoadScene("curling");
- }
-
- }
OnTriggerStayでストーンがあるか調べます。ストーンが無いときはDrawと表示したいですが、ストーンが無いとOnTriggerStayの処理が実行されません。
なので、スタート時にDrawと表示して、ストーンがある場合は、勝敗の表示に差し替えるようにしました。
カウントダウンにはコルーチンを使います。
- private IEnumerator CountDown()
- {
- yield return new WaitForSeconds(0.4f);
- int second = 5;
-
- while (second > 0)
- {
-
- restartText.text = second+"秒後に再読み込みします";
-
- yield return new WaitForSeconds(1.0f);
- second--;
- }
-
- SceneManager.LoadScene("curling");
- }
while文の繰り返し処理を1秒おきにやります。それが終わるとシーンを再読み込みします。