zimethsuki

GameManager.cs

Jul 11th, 2020
1,476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.84 KB | None | 0 0
  1. using System.Collections;
  2. using TMPro;
  3. using UnityEngine;
  4.  
  5. public class GameManager : MonoBehaviour
  6. {
  7.     public Animator UIanimator;
  8.     public Rigidbody[] ballRBs;
  9.     public TMP_Text timeScaleText;
  10.     public TMP_Text countdownText;
  11.  
  12.     private bool uiOpen = false;
  13.     private bool inCountdown = false;
  14.     private int countdownSecond = 0;
  15.  
  16.     // Update is called once per frame
  17.  
  18.     private void Start()
  19.     {
  20.         countdownText.text = "";
  21.     }
  22.  
  23.     private void Update()
  24.     {
  25.         if (Input.GetKeyDown(KeyCode.Escape))
  26.         {
  27.             if (!uiOpen && !inCountdown)
  28.                 PauseFunction();
  29.             else if (uiOpen && !inCountdown)
  30.                 ResumeFunction();
  31.         }
  32.  
  33.         if (Input.GetKeyDown(KeyCode.F))
  34.         {
  35.             if (!uiOpen && !inCountdown)
  36.                 AddForceToBalls();
  37.         }
  38.  
  39.         timeScaleText.text = "TimeScale: "+Time.timeScale;
  40.  
  41.     }
  42.  
  43.     private void AddForceToBalls()
  44.     {
  45.         foreach (Rigidbody rb in ballRBs)
  46.         {
  47.             rb.AddForce(rb.transform.up * 100f *Random.Range(2,5));
  48.         }
  49.     }
  50.  
  51.     private void PauseFunction()
  52.     {
  53.        
  54.         Time.timeScale = 0;
  55.         uiOpen = true;
  56.         UIanimator.SetBool("openUI", uiOpen);
  57.     }
  58.  
  59.     private IEnumerator ResumeGame()
  60.     {
  61.         uiOpen = false;
  62.         UIanimator.SetBool("openUI", uiOpen);
  63.         inCountdown = true;
  64.         countdownSecond = 3;
  65.  
  66.         while (countdownSecond > 0 )
  67.         {
  68.             countdownText.text = countdownSecond.ToString();
  69.             countdownSecond--;
  70.             yield return new WaitForSecondsRealtime(1f);
  71.         }
  72.  
  73.         inCountdown = false;
  74.         countdownText.text = "";
  75.         Time.timeScale = 1;
  76.     }
  77.  
  78.     public void ResumeFunction()
  79.     {
  80.         StartCoroutine(ResumeGame());
  81.     }
  82.  
  83. }
Add Comment
Please, Sign In to add comment